Time problem (SOLVED)

Started by Mel_O_Die, Fri 03/11/2006 13:49:34

Previous topic - Next topic

Mel_O_Die

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:

Code: ags

//////////////////////////////////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!
----( )----
Click to see "In Production" Topic!
[/color][/b][/url]

Gilbert

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).

Mel_O_Die

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
----( )----
Click to see "In Production" Topic!
[/color][/b][/url]

Khris

You could use the system time. It's accurate down to a second.
http://www.adventuregamestudio.co.uk/manual/DateTime.Now.htm

Mel_O_Die

thanks, but how can i use datetime for a timecounter? ??? ???
----( )----
Click to see "In Production" Topic!
[/color][/b][/url]

Khris

#5
In game_start():
Code: ags
DateTime*dt=DateTime.Now;
timecount=dt.Second;


In rep_ex_always():
Code: ags
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

Gilbert

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.

Khris

That's right, I forgot about that:

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

This should take care of it.

Mel_O_Die

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)
----( )----
Click to see "In Production" Topic!
[/color][/b][/url]

Khris

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?

Mel_O_Die

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:

Code: ags

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


in game start i put your lines of code

Code: ags

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


in repeatedly execute allways i have these ones for time

Code: ags

//////////////////////////////////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

Code: ags

  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 :)

----( )----
Click to see "In Production" Topic!
[/color][/b][/url]

Pumaman

Your original script to use the game loops looks like it should work, can you describe in more detail what the problem is?

Mel_O_Die

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
----( )----
Click to see "In Production" Topic!
[/color][/b][/url]

Mel_O_Die

i just see the cause of the problem, but still can't fix it:

Code: ags

  }
  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 ?
----( )----
Click to see "In Production" Topic!
[/color][/b][/url]

Khris

Hell, you're right, my bad.
It's supposed to be "timecount=dt.Second;"
Damn.

Mel_O_Die

yeah! it works fine! thanks so much!
----( )----
Click to see "In Production" Topic!
[/color][/b][/url]

Khris


SMF spam blocked by CleanTalk