How to update GameplayKit entities and components in a SceneKit Game - scenekit

I have a SceneKit game that I'm attempting to integrate with GameplayKit.
In order for GameplayKit to work, I need to update my components and entities by calling their updateWithDeltaTime(seconds:) method.
In the SpriteKit/GameplayKit examples I've seen, this is done using the SKScene's update method which fires continuously with no input from the user or the developer.
It seems that in SceneKit, to achieve something similar I need to call the SCNSceneRendererDelegate method renderer(renderer: updateAtTime time:).
However, this only fires when something in the scene is running, such as an SCNAction. But in my game, nothing is running until the user swipes.
Is there another, recommended SceneKit way to hook into some sort of run loop that runs continuously without any sort of prompt?

By default, SceneKit doesn't run the animation loop unless it knows there's something animating. That can include actions, animations, or physics.
If you want the animation loop to run without any of those things going on, set the view's playing property to true.

Related

How to make WPF game to draw as fast as you can

I am new to WPF and I want to make a program that is like a game. The common technique I see is to update your game in a dispatch timer (say every 10ms). But this is not ideal for me.
In XNA's update method, there is a 'elapsed' time parameter. Knowing how much time the last frame actually used is very helpful for me to decide what I do in the next frame. Is there any similar technique in WPF?
For some reason I have to use WPF and cannot use XNA.

Adding Canvas children in a for loop don't get displayed till loop is done

What the tittle says, I have lengthy for loop in which based on some conditions I add objects to a Canvas. I want the objects to show as they are added but they show after the loop is done? Does anyone know why and how I can fix this?
Also: the textbox.text property doesn't get updated too.
The UI won't update while you are tying up the UI thread, you need to return control to allow it to render.
You should try to move long-running operations to a background thread if possible - say using a BackgroundWorker - and update the UI every so often. This should result in a much more responsive UI.
If you really need to do some lengthy work on the UI thread (eg you're adding a large number of controls that are slow to render) you'll have to break it up into manageable portions. You can wait for the CompositionTarget.Rendering event to know when the UI has rendered and you can continue. But it's much better to offload work to a background thread if you can.

How to block input while WCF Asynchronous call runs?

I'm working on an application on Windows Phone, and I am using silverlight.
I have some bugs where the user can quickly press a button twice, which will effectively do 2 WCF calls since the action is called 2 times.
The obvious solution is simply to disable the button until the call completes but I'm wondering if there's a more global solution where I wouldn't have to implement this for every action. My application uses about 50 WCF methods so it would be tedious to implement this for every single action/every screens.
There's also the situation where users can click the phone back button while a call is running and start clicking on other buttons etc...
Anyone know a clean solution for this?
Simple solution: use a boolean variable that will be set to true after first click and to false when result from server has arrived back. In click handler just check the value of this variable, if it is true do not call the service again.
Are you using a design pattern like MVVM in your application? If not, you probably should.
In this case you could create a bool property called IsIdle or something like it. Then just set <Button IsEnabled="{Binding IsIdle}".
Now whenever you start doing an asynchronous call, set IsIdle to false. This will disable the buttons. When loading is finished set it back to true, so all the buttons are enabled again.
This might not be very elegant, but I think it would prevent any input without creeping all over your views and code.
You could prevent input by temporarily overlaying a transparent rectangle over your screen with IsHitTestable = true. You could write a small utility method to push that rectangle on any of your screens and remove it when you want to restore input.
You could also use that screen to eventually show something like a busy indicator when the network is slow.

Change mediaelement Source Based on Actions

I have a WPF 4 and VB.net 2010 project. I am playing videos in a single mediaelement. This is what I need to do:
When the window first opens, I have the first video play just fine. However, it is after this video plays that I run into trouble figuring out how to do the following.
I need the video source to change immediately following a single play through of any video, and I need this video (henceforth referred to as an "ambient" video) to loop forever.
When a certain event happens, I need to change the video source again, have it play once through, and then go back to looping ambient video in step 1.
Here is the rub, however. Many of the video triggers are inside of If-Then or Select Case statements in code behind, so I'm not exclusively using simple WPF events such as "MouseUp" or "MouseEnter".
Also, all videos must play in the same mediaelement, for performance reasons.
Thank you in advance!
How do I do this?
The Source of the media element is a DependencyProperty, as such any changes to it will be immediately reflected in the UI.
If you combine this with the MediaEnded event that is fired, you can set the Source and your problem is solved.
When you hit the triggers in code, you can either call a method or fire an event. You will have to use some semblance of a State Pattern to deal with the other logic. As an aside, check out Programming Without Ifs, it's an awesome intro on how to avoid insane conditional logic.
I set the mediaelement's LoadedBehavior to "Play" and Unloadedbehavior to "Stop", then I was able to just change the source of the mediaelement itself in code, and put the video I needed played after every video into the MediaEnded event.
It turns out that MediaEnded does not fire automatically when the LoadedBehavior is set to "Manual", unless "Stop" is explicitly called in code.
I hate accepting my own answers, so Nate Noonen gets the bounty (he was going down the right alley originally)! TY!

WPF Newbie question

I'm trying to learn WPF animations and am currently confused by quite a few things:
I used tools like processing, where you have a simple method which is called n times per minute, where n is the frame rate.
The way to do animations in WPF is to modify a property. If i use for example DoubleAnimation then a double is increased as the animation proceeds. But this is not exactly what I want. I want that in every cycle some properties are increased, some are modified by random and some are modified by user interaction. How can I do this in WPF?
What is also confusing me is the fact that WPF supports multiple animations at the same time. How does this work? Is there a thread for every animation or just one for all animations.
I used gdi with c# some time ago. I even could use multiple threads for drawing; As far as I remember I just had to insert all the drawing commands in some queue and then windows took care of them.. I have no idea how this is handled with WPF.
On a basic level, WPF animations are just the same as any other kind of animation: internally a timer ticks and some properties are modified which lead to a different picture when drawn to the screen.
WPF does all the leg work for you to be able to specify animations relative to wall-clock time, like "move that box at 3mm per second to the left". For more complex scenarios you might want to code up your own Animation, see the Custom Animation Overview article on the MSDN.
Regarding threading, WPF works the same as GDI: There is one Thread that handles all the interaction with the WPF model and you can only talk to WPF Controls if you're running on this thread. You can use the Dispatcher to "send" code to this thread if you are free threading. Actual drawing to DirectX is done in a separate thread, but that is of no concern to casual users of the API.
You can run several animations at the same time by putting them into a StoryBoard.
You can use the animation's BeginTime to get one animation to start after another.
You can use the key frames version (DoubleAnimationUsingKeyFrames) or the path version (DoubleAnimationUsingPath) to create complex non-linear animations.

Resources