Game Development Community

Need help with TotalElapsedTime and a timeline

by John Bura · in Torque X 2D · 06/03/2010 (7:48 am) · 1 replies

Hi there, Sorry for all the noob questions I recently switched to torque and Im having a hard time adjusting because some of the stuff isn't the same as the basic XBOX game template.

What Im trying to do is set up events in Game time. Therefore, at 3 seconds into the game I would like to spawn something or change a graphic.

So basically Im looking to set up a timeline of events.

Any help would be awesome :)

#1
06/03/2010 (10:25 am)
For something like this I made a gameControllerComponent. There are probably other ways to do it but that's what I did. you can attach it to a background element or I made a blank scene object.

in ProcessTick I would add the elapsed time to a running time counter. I would also compare this to a list of events to fire off timed events.

the list is like this
public TimeEvent(float eventTime, eDelegate eventFunc)
{
     _eventTime = eventTime;
     _eventFunc = eventFunc;
}

The eventTime is a float that has the time for the event to fire in seconds, the eventFunc is a function delegate to be called when the event fires.

I make a list of events:
List<TimeEvent> eventList;

I have a method to fill the list in a controlled random way. The times are all relative, they could be absolute however I made them relative, like 1.5, 2.25, 2.6, 1.74, 3.2 this would be the seconds elapsed since the previous event.

Then in the control component it looks like this.

public virtual void ProcessTick(Move move, float dt)
{
    _currentTime += dt;
    if (_currentEvent < _eventCount && _currentTime >= eventList[0].EventTime)
    {
        doEvent();
     }
}

In doEvent() I fire off the delegate, then remove the event from the list, as I will not need it again. This way I only have to track the first event in the list. Then set _currentTime = 0; so it starts counting up the time again.

You could also never set _currentTime to 0 which would give you total elapsed time, etc etc.

you can also get the game time via TorqueEngineComponent.Instance.GameTime.ElapsedGameTime or ElapsedRealTime

like this:
_totalTime += TorqueEngineComponent.Instance.GameTime.ElapsedGameTime.Milliseconds;

I believe the TorqueEngineComponent.Instance.GameTime is the same as the microsoft XNA bits.