Game Development Community

Getting remaining time on a scheduled event

by Corey Martin · in Torque Game Builder · 08/31/2005 (6:43 am) · 6 replies

I have need of determining the remaining time on a schedule event from within the scripts. I ran a search through the GG forums and found a post with, what looks like, exactly what I need. Problem is, its for TGE. There are some resources and code snippets you can use from within both T2D and TGE, I know, but being ignorant of C++ and also unable to find the mentioned files (simbase.cc, simmanager.cc), i'm not to sure if this solution is a possibility for me. Any clarification on this or a different solution would be fantastic. :)

#1
08/31/2005 (8:36 am)
Heres the posts from the thread you probably found :)


Posted by: Vijay Myeni

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.
#2
08/31/2005 (8:37 am)
Then another post by: Ramen Sama

@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 li
st.

   [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]
#3
08/31/2005 (8:37 am)
And another post on how to use it by: Ramen Sama

***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.
#4
08/31/2005 (9:00 am)
If you aren't comfortable editing C++ files you can use the following method.

function getScheduleDuration(%schedule)
{
	%return = %schedule.endtime - %schedule.starttime;
	return %return;
}

function getTimeSinceStart(%schedule)
{
	%return = getsimtime() - %schedule.starttime;
	return %return;
}

function getTimeRemaining(%schedule)
{
	%return = %schedule.endtime - getsimtime();
	return %return;
}

function scheduleWithTime(%time,%obj,%function)
{
	%schedule = schedule(%time,%obj,%function);
	%schedule.starttime = getsimtime();
	%schedule.endtime = getsimtime()+%time;
	return %schedule;
}

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=scheduleWithTime(10000,0,bingbing);
CalculateFill($SuperTimer);

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

NOTE: I'm at work so I haven't had a chance to test my code for errors... but that's pretty much how it could be done :)
#5
08/31/2005 (9:05 am)
Thx Harold, much more friendly solution :) (better in my opinion too)
#6
08/31/2005 (10:33 am)
Wow. You guys rock my world, in a big way. Big, huge thanks to both of you for the help! Hopefully, this thread will also help other people with a similar need who don't have access to the TGE forums!