1
\$\begingroup\$

In Games I often run into this kind of scenario:

The Game Logic decides something happens, and of course the logic does so instantly. But the Game also has animations depending on the logic. Like player died -> play death animation -> respawn. But the logic can't wait for the animation so we have to work around that.

Or take for example Starcraft 2 siege tanks as morphing units on click and they start the siege animation, but in what state is the logic. Is there a state for each animation too? Is it just a Flag ignoring everything as long as it is animating?

It tends to become really messy as i am including more and more animations.

Hope someone can give me a hint.

\$\endgroup\$
2
\$\begingroup\$

For engines written by you in native code

  • Separate your render logic out. Every cycle, run all your game / simulation logic in one phase, and once that's all complete, run all rendering logic -- this includes animation.
  • Since rendering and animation are separate, run game logic timers that use the same duration as the animations, but only have game logic react to the completion of the logic timer, not the animation timer. Example follows:

    • (1st tick) gameLogic.startProcess(myDuration). remainingDuration is in the data model.
    • (1st tick) renderer.startAnimation(myDuration).
    • (all ticks between 1st and final) game logic checks whether process is ready to expire by comparing time remaining against duration.
    • (all ticks between 1st and final) animation proceeds.
    • (final tick) Game Logic completes. Take game logic actions accordingly.
    • (final tick) Animation completion coincides with logic completion. No callback is required because completion was already handled in model. (You'll need to know what the animation's duration is in order to achieve this.)

...Thus because you've smiultaneously started a game logic process and a render process with the same duration, they will finish at the same time, but you don't need render logic to notify game logic that the animation is complete, because the game logic already knows by looking at myDuration in the model.

For existing engines / platforms like Unity

...or Flash, where render and game logic is mixed and events are common, best stick with the approach that is most commonly used, by chaining render logic back to game logic using an event. It is not worth the effort to go against the architectural paradigms inherent in the core language / platform, it will simply make your life difficult.

About animation

Again depending on language / platform, animation may or may not be handled automagically for you. Flash MovieClips, for instance, will handle this.. Unity works similarly if you use out-of-box solutions for animation, JS also whenever you use setInterval or requestAnimationFrame. Nevertheless, bear in mind that at some fundamental level, usually in native code, there is always a loop that is running and doing all of this for you, whether it is animation or your main game loop updates on GameObjects, Sprites or whatever. If you are already writing native code, then you will know this from working with your main application loop.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I am a big fan of decoupling etc. and I have been using it for a long time now. Normally i go by model changes -> view receives event and start animation etc. that's all fine but i still don't can't grasp the other way round, what if I want the animation to finish before some logic changes again. Do you have a concrete example for the third point? Do you mean a timer in the model that basically stops the logic from processing during animation time? \$\endgroup\$ – user1090755 Oct 4 '14 at 11:41
  • \$\begingroup\$ @user1090755 If I understand your final question correctly, then yes. Adding an example to the answer now. P.S. One has to be very careful not to adversely affect ordering when using events. What are you writing your code in? \$\endgroup\$ – Engineer Oct 4 '14 at 11:49
  • \$\begingroup\$ Well it's more of a general question as you can adapt the concepts for every language. Currently it would be c# and unity. So if I understand it correctly I would introduce a Flag stopping the relevant logic from reacting while the animation plays. That would mean i have to know the length of the animation playing right? \$\endgroup\$ – user1090755 Oct 4 '14 at 12:04
  • \$\begingroup\$ @user1090755 It's not possible to answer this question generally because it's approached differently based on loop vs. event based models in different programming platforms / architectures. I would ordinarily recommend a best-practice approach as above, but Unity does not typically follow that and instead commingles game and render logic (unfortunately). So with Unity, I would strongly suggest you simply let the animation completion fire an event to notify game logic; this may result in an immediate sequence of function calls, or it may simply set a flag to be acted upon in the next update. \$\endgroup\$ – Engineer Oct 4 '14 at 12:12
  • \$\begingroup\$ TLDR; in native-coded engines, it's best to use the approach given above to keep things clean, decoupled and well-ordered. In Unity or Flash where rendering and game logic are inherently mixed, best stick with the approach that is most commonly used, by chaining render logic back to game logic using an event. \$\endgroup\$ – Engineer Oct 4 '14 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.