Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Mel_O_Die on Fri 03/11/2006 13:49:34

Title: Time problem (SOLVED)
Post by: Mel_O_Die on Fri 03/11/2006 13:49:34
Hi!

I've a little problem with my "time of play" counter, i have it on a gui to show how many time the player has spend on the game.

i have that code in my repeatedly execute allways part:


//////////////////////////////////TIME////////////////

timecount--;
if ( timecount==0 ) {  // Once the timer has expired (one second)
  second++;                     // Increment seconds
  if ( second == 60 ) {      // Increment minutes and reset seconds if needed
    second = 0;
    minute++;
  }
  if ( minute == 60 ) {      // Increment hours and reset minutes if needed
    minute = 0;
    hour++;
}
  timecount=GetGameSpeed();      // Restart the timer
 
}
 
counter.Text = String.Format("%02d:%02d:%02d", hour, minute, second);
counter2.Text = String.Format("%02d:%02d:%02d", hour, minute, second);


My problem is the following: when changing room, time bouncing up for approximatively 30 seconds (for an action who takes only a second or two)

do you have any idea to fix it?

thanks!
Title: Re: Time problem
Post by: Gilbert on Fri 03/11/2006 14:12:27
The quoted codes are in the global script right?
Are there any other places that have reference to those time variables (timecount, second, minute, hour), check especially events for changing rooms, and those entering room events.
Also, it can also cause troubles when there's a skippable cutscene (as when a cutscene is skipped it's run at maximum speed your computer can handle,not regulated to the "gamespeed" fps).
Title: Re: Time problem
Post by: Mel_O_Die on Fri 03/11/2006 14:59:38
i've allready checked this, the code is in global script & execute allways
time is only used for a gui indication
and the problem is here at any changeroom skippable or not

but you say "run at maximum speed your computer can handle,not regulated to the "gamespeed" fps"

probably it's the same when a room is leaved and fade to an other, the fps can be high so it will explain the jump of time, but, how can i fix it... aw i really don't see anything now
Title: Re: Time problem
Post by: Khris on Fri 03/11/2006 17:18:25
You could use the system time. It's accurate down to a second.
http://www.adventuregamestudio.co.uk/manual/DateTime.Now.htm
Title: Re: Time problem
Post by: Mel_O_Die on Sat 04/11/2006 13:07:39
thanks, but how can i use datetime for a timecounter? ??? ???
Title: Re: Time problem
Post by: Khris on Sat 04/11/2006 15:46:27
In game_start():
DateTime*dt=DateTime.Now;
timecount=dt.Second;


In rep_ex_always():
DateTime*dt=DateTime.Now;
int s=dt.Second;
if (s<timecount) s=s+60;
int d=s-timecount;
if (d>0) {
  second=second+d;
  if (second>59) {
    second=second-60;
    minute++;
    if (minute==60) {
      minute=0;
      hour++;
    }
  }
  timecount=dt.Second;   // edit: fixed this line
}

Not tested, but looks like it should work.
The code even catches a jump of more than one second, though that probably won't happen.

EDIT: fixed coding error
Title: Re: Time problem
Post by: Gilbert on Sat 04/11/2006 16:06:49
One possible problem with using system time is that you need to incorporate some handling codes in case the player loads a saved game, I'm just too busy to invest on this at the moment.
Title: Re: Time problem
Post by: Khris on Sat 04/11/2006 17:14:57
That's right, I forgot about that:

// global script
on_event(EventType event, int data) {
  if (event==eEventRestoreGame) {
    DateTime*dt=DateTime.Now;
    timecount=dt.Second;
  }
}

This should take care of it.
Title: Re: Time problem
Post by: Mel_O_Die on Sun 05/11/2006 17:45:52
it's almost working but there is few other problems:

- The seconds in the game are the real windows seconds (if the game is launched at 6H15m32s, the ingame time of play will display 0H0m32s at the start, perfectly synchronised with windows time :s, i wish for the seconds starts to zero

- There is a problem with loading too, for example ,if i save my game at 2min and 15sec and if i load it at 5min and 40sec, the game loaded time is 2min and 40sec (because of windows synchronisation)
Title: Re: Time problem
Post by: Khris on Sun 05/11/2006 18:05:13
That's a weird error, and I can't imagine how my code would produce it.
Did you put a "second=dt.Second;" somewhere accidentally?

What my code does:
-store current system seconds in timecount
-repeatedly check if system seconds have advanced
-if they have, increment second accordingly (normally by 1)

Could you post your code, unless you're absolutely sure it's the same?
Title: Re: Time problem
Post by: Mel_O_Die on Sun 05/11/2006 21:44:27
i have checked all and i don't find where is the cause of the problem

first, at the beginning of the global script, just before game_start section, i declare the following variables:


// main global script file
int second;
int minute;
int hour;
int timecount;


in game start i put your lines of code


DateTime*dt=DateTime.Now;
timecount=dt.Second;


in repeatedly execute allways i have these ones for time


//////////////////////////////////TIME////////////////

DateTime*dt=DateTime.Now;
int s=dt.Second;
if (s<timecount) s=s+60;
int d=s-timecount;
if (d>0) {
  second=second+d;
  if (second>59) {
    second=second-60;
    minute++;
    if (minute==60) {
      minute=0;
      hour++;
    }
  }
  timecount=second;
}
   
counter.Text = String.Format("%02d:%02d:%02d", hour, minute, second);


and finally, in on_event section


  if (event==eEventRestoreGame) {
    DateTime*dt=DateTime.Now;
    timecount=dt.Second;
  }


the variables "second" and "timecount" appears only into these lines, i've made script searches to find if eventually i "put a "second=dt.Second;" somewhere accidentally" and the answer is no :)

Title: Re: Time problem
Post by: Pumaman on Mon 06/11/2006 19:03:44
Your original script to use the game loops looks like it should work, can you describe in more detail what the problem is?
Title: Re: Time problem
Post by: Mel_O_Die on Mon 06/11/2006 19:54:43
i'm using ags2.72 (maybe it's important, i forgot to precise that  :-\)

and the problem is that the seconds displayed  on my counter are exactly the windows seconds in real time, if i launch the game at xhour xminute and 50seconds (for example) my counter will show 50seconds and run in perfect synchro as windows time, when windows clock reach a minute, a minute is incremented in the game

i can't find where is the problem of the code because it seems good to me too, it makes me mad :D
Title: Re: Time problem
Post by: Mel_O_Die on Mon 06/11/2006 20:15:38
i just see the cause of the problem, but still can't fix it:


  }
  timecount=second;
}
   
counter.Text = String.Format("%02d:%02d:%02d", hour, minute, second);


timecount int was supposed to be the reference of the realtime, why we had to change it equal to second int ?
Title: Re: Time problem
Post by: Khris on Mon 06/11/2006 20:46:37
Hell, you're right, my bad.
It's supposed to be "timecount=dt.Second;"
Damn.
Title: Re: Time problem
Post by: Mel_O_Die on Mon 06/11/2006 22:37:38
yeah! it works fine! thanks so much!
Title: Re: Time problem (SOLVED)
Post by: Khris on Mon 06/11/2006 22:52:34
Glad it's working :D