Custom timer

Started by Gamer_V, Tue 24/02/2009 01:07:09

Previous topic - Next topic

Gamer_V

Why hello there! :)

I needed a timer for my game but since you can't check the status of a SetTimer until it's done I had to make one myself. AGS has 40 cycles per second so I just needed a counter that goes to 40. (There's an example in the manual, but only under IsButtonDown, and on the forums) So this is in my roomscript where I need this timer. I've boiled it down to this simple form in which it should just show "Second" every ten seconds but it just shows "Second" 10 times right after each other. I'm probably doing something silly (for the last hour or two), but I can't quite figure out what.

Code: ags
function rhytm(int beat)
{
  int counter=0;
  while(counter <= beat)
  {
  counter++;
  }
Display("Second")  ;

}  

function room_AfterFadeIn()
{
Display("Lololol");
rhytm(400);
rhytm(400);
rhytm(400);
rhytm(400);
rhytm(400);
rhytm(400);
rhytm(400);
rhytm(400);
}


Gilbert

Because you missed a Wait(), otherwise the game loop won't be updated, i.e.
Code: ags
function rhytm(int beat)
{
  int counter=0;
  while(counter <= beat)
  {
  Wait(1);
  counter++;
  }
  Display("Second")  ;
}  
}


However, unless you want to do something within the loop, the above function could just be shorten to:
Code: ags
function rhytm(int beat)
{
  Wait(beat+1);
  Display("Second")  ;
}  
}

(Note that in your original while loop you use <= for comparison, so it will run from 0 to beat, altogether 'beat+1' loops, which may not be what you initially want, so, substitute 'beat+1' as 'beat' if necessary.)

Also, the codes are blocking, if you want to have non-blocking codes you may need to do things in some other ways.

Gamer_V

#2
Thanks! :) It's supposed to become a sort of rhytm game, I'll see how far I get, but it will do something in the loop:

Code: ags
function rhytm(int beat)
{
  int counter=0;
  while(counter <= beat+20)
  {
  Wait(1);
  counter++;
    if (IsKeyPressed(eKeySpace)) 
    {
      //do something depending on the current countervalue (how close it is to the beat)
    }
  }
} 


I'll probably run into more problems with this later, but 3 am is not a good time to be coding anyway. Anyway, thanks!

SMF spam blocked by CleanTalk