Game Development Community

getting the time of day

by Spencer · in Torque 3D Professional · 11/29/2011 (1:49 am) · 11 replies

I was looking into the time of day system and looking specifically at scripting events to occur during certain times. Im already using the time of day events to trigger events at certain times, but I want to have my AI check the time to determine their actions, but it doesnt appear that it is exposed to script. Is there some other way to get the current timeOfDay time? Or can someone help me write a function to get the current time? This is my first real stab at programming so Im kind of clueless...
Thanks in advance!

#1
11/29/2011 (8:42 am)
You might want to check this for some general information about
using the time features as well..

www.garagegames.com/community/forums/viewthread/91245
#2
12/01/2011 (7:58 am)
Thats some very useful stuff, but im trying to get the 0-1 time value created by the timeOfDay object. I should have been more specific. Thanks for your help though! Does anyone know how to get the "time" value created by the timeOfDay object? Thanks in advance!
#3
12/02/2011 (8:03 am)
Hadnt look at that before, but its not a simple one due to the following:

else if ( mPlay )
   {
      F32 dt = TickSec;
      F32 current = mRadToDeg( mNextElevation );

      if ( current > 350.0f || ( 0.0f <= current && current < 190.0f ) )
         dt *= mDayScale;
      else
         dt *= mNightScale;

      mTimeOfDay += dt / mDayLen;

      // It could be possible for more than a full day to 
      // pass in a single advance time, so I put this inside a loop
      // but timeEvents will not actually be called for the
      // skipped day.
      while ( mTimeOfDay > 1.0f )
         mTimeOfDay -= 1.0f;

TimeOfDay is a Float 0-1
So it doesnt actually calculate it in hours...
You would have to add in some C++ just to get that value like say:

//timeOfDay.h
public
   //Mythic DEBUG
   const F32 getTimeOfDay( ) const { return mTimeOfDay; };

//timeOfDay.cpp
DefineEngineMethod( TimeOfDay, getTimeOfDay, F32, ( ),,
   "" )
{
   return object->getTimeOfDay( );
}

That will at least give you the current Value (0-1)
But you would then have to do some calcs to figure out the actual Hour.

I can see they were in the process of adding that were I found this..
//addVariable( "$TimeOfDay::currentTime", &TimeOfDay::smCurrentTime );
But the smCurrentTime is not used yet, so you would have to write your
own code to do the calcs in C++ then set the Variable.

*edit* I'm trying to do too many things at once, my above little
code snippet is what ya want to get the float value :) But you need to
add it to the Source code.
#4
12/02/2011 (8:59 am)
Thant is exactly what im after! You are my hero. C++ (or any programming really, including torque script.....) is totally foreign to me. I want to rewrite the time of day system, and this was my first step. Thanks for your help! :)
#5
12/02/2011 (2:46 pm)
You wont be the first, do a little digging into the forums/resources..
There have been several mods to Night/Day systems over the years.
Good luck and have fun..
#6
12/05/2011 (5:32 pm)
Ok, so I've added the code you suggested and it compiles without error, however I cant seem to get it to work in game. Its obviously an error in my code trying to access it. I have this in the scripts/server directory:
function TimeOfDay::onAdd( %this )
{
   %this.addTimeOfDayEvent( 0, "Sunrise" );
   %this.addTimeOfDayEvent( 180, "Sunset" );
}
$DayCount = 0;
function TimeOfDay::onTimeEvent( %this, %identifier, %currentTime, %currentElevation )
{
   //echo( %identifier );
   
   if( %identifier $= "Sunrise" )
   {
      $DayCount = $DayCount++;
      echo( $DayCount );
      getTimeOfDay();
   }
}

function TimeOfDay::getTimeOfDay(%this)
{
   echo(%this);
}

What would be the correct way to call this function from torque script? Thanks for all your help so far!
*edit: This is just my latest attempt. I've tried a lot of different ways with no results. I even tried typing it directly into the console with the id of the timeOfDay object. Nothing works. Thanks!
#7
12/05/2011 (6:27 pm)
To access the Function (c++) you need the Object (timeofday)..
but in your code you are creating an additional function that
doesnt fit with the setup..

so for instance..
function TimeOfDay::onTimeEvent( %this, %identifier, %currentTime, %currentElevation )  
{  
   //echo( %identifier );  
     
   if( %identifier $= "Sunrise" )  
   {  
      $DayCount = $DayCount++;  
      echo( $DayCount );  
      getTimeOfDay();  
   }  
}
Change to:
function TimeOfDay::onTimeEvent( %this, %identifier, %currentTime, %currentElevation )  
{  
   //echo( %identifier );  
     
   if( %identifier $= "Sunrise" )  
   {  
      $DayCount = $DayCount++;  
      echo( $DayCount );
  
      echo(%this.getTimeOfDay());
   }  
}

[ %this ] is the TimeOfDay Object that has Access to the C++ code call getTimeOfDay()
as per this: DefineEngineMethod( TimeOfDay, getTimeOfDay...
That should work. You dont create a Script function for it...

Not workable (this is a script function):
function TimeOfDay::getTimeOfDay(%this)
{
echo(%this);
}

The getTimeOfDay() function is just like the addTimeOfDayEvent() functions.
#8
12/05/2011 (6:42 pm)
ooooooooooooooooooh! I get it. I was writing it as a function but its an engine method. That makes total sense now. I was pouring through the source looking for something similiar but without knowing what I was looking for I was lost. Thank you so much for all your help!
*Edit: Its not working... I changed the code and am getting this error,
scripts/server/IsolationTest.cs (19): Unknown command getTimeOfDay.
Object time(4278) time -> TimeOfDay -> SceneObject -> NetObject -> SimObject
#9
12/06/2011 (12:02 am)
Well, not sure then, sounds like it's not coded right, (C++).
Will test it out later, but it should work fine, fairly simple addittion.
Try running it in Debug with a Break in C++ at that code segment.
#10
12/06/2011 (2:50 pm)
Wow... i feel pretty dumb. I forgot to compile a release build. I forgot to change it from debug... It works great now! Thanks for your help!
#11
12/07/2011 (10:00 am)
If you're on the server and working from script (seems like both of these things are true from the errors above) you can also grab the time value directly off the object. You have to make sure you're naming your Time Of Day object something unique; I always call mine TimeODay, in which case I can access the time value with:

TimeODay.time
for example: echo(TimeODay.time);

Make sure to check for null values before using it, just as a failsafe in case the object isn't named correctly in your mission file.