Game Development Community

dev|Pro Game Development Curriculum

Script Time

by Richard_H · 11/13/2006 (4:44 pm) · 0 comments

A simple way to add a console function ( getSysTime() ) for getting the time in seconds since midnight from the system clock. Note: This is my first resource and is kind of basic.

1. First you need to add the time header file (),
open up main.cc and go to the end of the #includes and add
#include <time.h>

2. Next we need to actualy add the console function,
go down past the 2 existing time functions
ConsoleFunction( getSimTime, S32, 1, 1, "Return the current sim time in milliseconds.\n\n"
                "Sim time is time since the game started.")
{
   return Sim::getCurrentTime();
}

ConsoleFunction( getRealTime, S32, 1, 1, "Return the current real time in milliseconds.\n\n"
                "Real time is platform defined; typically time since the computer booted.")
{
   return Platform::getRealMilliseconds();
}
and add
ConsoleFunction( getSysTime, int, 1, 1, "Get the time in seconds.")
{
  time_t rawtime;
  struct tm * timeinfo;
  int temp;
  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  temp = timeinfo->tm_sec;
  temp = temp + (timeinfo->tm_min*60);
  temp = temp + (timeinfo->tm_hour*3600);
  return temp;
}
Basicly, what this does is get a tm struct and convert the hours and minutes to seconds,
then add them to the seconds then return the result.

To test if it worked just type the following in the console:
echo(getSysTime());
You should get the number of seconds since midnight.
Please comment so I can make my next resource better.