Game Development Community

To get the time remaining in a scheduled event

by Vijay Myneni · in Torque Game Engine · 01/27/2005 (6:43 am) · 9 replies

I remember someone asking for this on another forum a while back. I'm still not sure if this already exists, but I went ahead and wrote a quick method to get the time remaining before a scheduled event will go off:

in simbase.h, near where isEventPending is declared:
U32  getTimeRemaining(U32 eventId);

in simbase.cc, near where the consolemethod for isEventPending is declared:
ConsoleFunction(getTimeRemaining, S32, 2, 2, "getTimeRemaining(%scheduleId);")
{
   argc;
   S32 ret = Sim::getTimeRemaining(dAtoi(argv[1]));
   return ret;
}

and in simManager.cc, near where isEventPending is implemented:
U32 getTimeRemaining(U32 eventSequence)
{
   for(SimEvent *walk = gEventQueue; walk; walk = walk->nextEvent)
      if(walk->sequenceCount == eventSequence)
         return (walk->time - getCurrentTime());
   return 0;
   
}

Then, you can call it from script the same way you would call...yup, you guessed it, isEventPending()!

I tested it, but I haven't really found much use for it yet in my game. Let me know if there are issues.

#1
01/27/2005 (5:33 pm)
Cool. Probably worth adding to HEAD, so it's on my list now!
#2
03/14/2005 (10:40 pm)
Ok, it's in. Thanks, Vijay!
#3
07/14/2005 (8:44 pm)
Wow, thanks alot! i desparately needed something like this. You saved the day!
#4
07/14/2005 (9:05 pm)
No problem. I'm glad someone's using it. I've found it pretty useful since I wrote it as well.
#5
07/15/2005 (12:40 am)
Also a good one. In the fix database.
#6
07/15/2005 (4:32 am)
@Ben sorry, in my haste i posted some code that really didn't do anything and deleted it.
Here's to make up for it.


Here are two new functions to get information on a schedule. One Is GetScheduleDuration, which tells you in MS how long the schedule is set for. The other is GetTimeSinceStart, which will tell you in MS how long the schedule timer has been running.

This code can be used in conjuction with the above code for getting the remaining time of a schedule, or it can be used without it. New code is in bold. Note i've included the above function "getTimeRemaining".
I won't put that in bold. Cause it's not required for this.

in simbase.h around line 105

public:
   SimEvent *nextEvent;     ///< Linked list details - pointer to next item in the list.
[b]
   //Ramen Sama is pretty cool
   SimTime startTime;       ///< Time when Schedule Started
   //Ramen Sama is pretty cool
[/b]
   SimTime time;            ///< When the event is scheduled to occur.

What i added what a way to get the start time of the schedule.


Also in simbase.h around line 1265:
void cancelEvent(U32 eventId);
   bool isEventPending(U32 eventId);
   U32  getTimeRemaining(U32 eventId);
[b]
//Ramen Sama Is Pretty Cool
   U32  getTimeSinceStart(U32 eventId);
   U32  getScheduleDuration(U32 eventId);
//Ramen Sama Is Pretty Cool
[/b]


next up is simbase.cc around 443 or so

ConsoleFunction(isEventPending, bool, 2, 2, "isEventPending(%scheduleId);")
{
   argc;
   return Sim::isEventPending(dAtoi(argv[1]));
}

ConsoleFunction(getTimeRemaining, S32, 2, 2, "getTimeRemaining(%scheduleId);")
{
	argc;   S32 ret = Sim::getTimeRemaining(dAtoi(argv[1]));
	return ret;
}
[b]
//Ramen Sama is pretty cool
ConsoleFunction(getScheduleDuration, S32, 2, 2, "getTimeSinceStart(%scheduleId);")
{
	argc;   S32 ret = Sim::getScheduleDuration(dAtoi(argv[1]));
	return ret;
}

ConsoleFunction(getTimeSinceStart, S32, 2, 2, "getTimeSinceStart(%scheduleId);")
{
	argc;   S32 ret = Sim::getTimeSinceStart(dAtoi(argv[1]));
	return ret;
}
//Ramen Sama is pretty cool[/b]

these are the console methods.. simple enough



in simManager.cc around line 69 in PostEvent
AssertFatal(destObject, "Destination object for event doesn't exist.");
[b]   event->startTime=gCurrentTime; //Ramen Hack[/b]
   event->time = time;




finally in simManager.cc around 141 or so

bool isEventPending(U32 eventSequence)
{
   for(SimEvent *walk = gEventQueue; walk; walk = walk->nextEvent)
      if(walk->sequenceCount == eventSequence)
         return true;
   return false;
}

U32 getTimeRemaining(U32 eventSequence)
{
	for(SimEvent *walk = gEventQueue; walk; walk = walk->nextEvent)
		if(walk->sequenceCount == eventSequence)
			return (walk->time - getCurrentTime());
			return 0;
}
[b]
//Ramen Sama is Pretty Cool
//How long in MS is the Schedule set for?
U32 getScheduleDuration(U32 eventSequence)
{
	for(SimEvent *walk = gEventQueue; walk; walk = walk->nextEvent)
		if(walk->sequenceCount == eventSequence)
			return (walk->time-walk->startTime);
			return 0;
}

//How long in MS Has is been since schedule began?
U32 getTimeSinceStart(U32 eventSequence)
{
	for(SimEvent *walk = gEventQueue; walk; walk = walk->nextEvent)
		if(walk->sequenceCount == eventSequence)
			return (getCurrentTime()-walk->startTime);
			return 0;
}
//Ramen Sama is Pretty Cool
[/b]
#7
07/15/2005 (4:33 am)
***continued******


Ok pretty easy. How's it work? Easy.
Here's a sample bit of my code.

function CalculateFill(%timer){


	%timeDuration=getScheduleDuration(%timer);
	%timeRunning=getTimeSinceStart(%timer);

	echo("Schedule Length :"@%timeDuration);
	echo("Schedule Time Running :"@%timeRunning);


	%percentageDone = (100*(%timeRunning/%timeDuration));
	echo("Percentage Finsihed : %"@%percentageDone);

	if (%PercentageDone==100)
		return;


	schedule(100,0,CalculateFill,%timer);

}

$SuperTimer=schedule(10000,0,bingbing);
CalculateFill($SuperTimer);

function bingbing(){
error("Timer has gone off");
}


So what use is this code? i'm planning on using it for filling a meter to let me know when my players wait time is over and what not. You should be able to resize things or do all sorts of things based on the timer.

please let me know if there are any obvious mistakes. It's actually the first bit of code i've donated to the community, so... Based mostly on the code above :) it works for me, but perhaps i copied something over wrong or forgot to copy something.
#8
07/21/2005 (10:21 pm)
#154 is resolved. Thank you, Ramen, nice code.
#9
02/13/2008 (4:00 pm)
These two new functions no long work without a quick fix for 1.5+

www.garagegames.com/mg/forums/result.thread.php?qt=72096