Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is update() preferred for game-logic rather than didFinishUpdate?

In the programming guide for SpriteKit the update() function is referred to as the best place to implement your own game-logic. But since when I realized centering a camera on a node works better in didFinishUpdate() (avoiding latency), I have been using that option.

camera.position.y = node.position.y

Scared of other latency problems I have also implemented the rest of my game logic. It works. Therefore, for what reason do they recommend update()? Is there performance benefits from using both methods?

Thank you.

like image 280
ecoguy Avatar asked Nov 19 '15 17:11

ecoguy


Video Answer


1 Answers

There is no performance benefit, it's really a matter of determining the preconditions of your game logic. Do you want to run your logic with the assumption that physics and actions WERE (didUpdate) calculated or WILL BE (update) calculated. For example update gets called before any physics and actions are processed. So if you had logic like custom physics calculations, spawning a wall to block a physics body, or even simply executing an action you want to run immediately then that should be done in update method because all of these cases expect that physics and actions WILL be calculated in the current frame. However centering a node on a camera would be good in the didUpdate because you want to center your camera AFTER the physics were calculated.

It's also important to note that the update method passes the current game time so you will need to use the update method to calculate the time between frames (AKA delta time). This is often necessary to do things like counting to perform logic after N seconds or for handling custom physics with a variable time step. This won't restrict you to the update method however because you should save the current delta time in a variable so you can access it from the didUpdate method and other callbacks.

From personal experience I find myself using the update method more than the didUpdate simply because I often have a lot of custom physics and logic that often assumes the changes I make will be processed all in the current frame. But as I said above it depends on what your preconditions are. And in some cases it won't matter what you choose because the logic may be independent of the rendering cycle.

I think this image from the Sprite Kit Documentation makes it much easier to visualize the rending cycle. Analyze your game logic and determine at what point in the cycle your game logic makes sense. enter image description here

like image 173
Epic Byte Avatar answered Sep 24 '22 01:09

Epic Byte