TX/Tutorials and Guides/TX UserGuide/Ticking and Processing
From TDN
Contents |
|
[edit] IntroductionTorque X provides a 2D game system based on the popular Torque Game Builder game engine (TGB). We've modified the TGB engine by adding the concept of components and breaking much of the functionality of the TGB classes into several components. [edit] ProcessTickGenerally, all movement and most game logic should be done in the ProcessTick method. A game object (or one of it's components) registers for a process tick callback by calling AddTickCallback(TorqueObject obj, ITickObject itick) on the process list (usually this is done in _OnRegister of a component). The callback is associated with 'obj' and will occur along with all the other process tick callbacks for that object. You can also supply an optional third parameter 'order' to specify the order this callback should occur compared to other callbacks for the object. An order of 0 means you want to be the first callback whereas a value of 1 mean you want to be the last. The default is 0.5. We saw an example of the ProcessTick callback when discussing how an object receives moves. The interface is: void ProcessList(Move move, float dt) The dt parameter is the elapsed time (in seconds) since this method was last called. This can be used to execute logic based on time passed. For example, if we want something to execute every second of time passed:
// How much time has elapsed since last execution time
private float _timeElapsed;
// How often we execute
private float _executeTime = 1f;
public virtual void ProcessTick(Move move, float dt)
{
_timeElapsed += dt;
if (_timeElapsed >= _executeTime)
{
// Execute code here
_timeElapsed = 0;
}
}
[edit] InterpolateTickTorque X supports both constant and variable time update. When a constant time update is used we get the advantage of much more consistent behavior between different machines (an update routine called 100 times a second can behave very different than one called 50 times a second, even when it's designed specifically to behave the same in both cases). The downside to this is that if a frame needs to be rendered between two ticks we need to be able to reposition everything to interpolate between the two ticks. That is achieved by the InterpolateTick callback. When this callback occurs, the object (or component) is responsible for interpolating between the last two ticks (1 corresponds to the latest tick whereas 0 corresponds to the one before). Most of the time you'll be using stock components that interpolate things like position and rotation so you won't need to do anything here. However, if you register for a tick callback you'll also need to implement the interpolate callback since InterpolateTick is part of the ITickObject interface. [edit] UpdateAnimationCertain things that a game object does don't care about constant ticking. Animation is the best such example. It would be quite tedious to track the state of an animation on each tick and then figure out the interpolated position when rendering. Instead, we update animation on the UpdateAnimation callback. Just before rendering, this callback is made with the elapsed time since the last render. To register for an UpdateAnimation callback you call AddAnimation callback on the process list. Like tick callbacks, you can supply a third optional 'order' parameter where 0 means first and 1 means last. |



