How do I represent a timer (in game ticks) as H:M:S?

Started by bx83, Thu 22/07/2021 10:41:11

Previous topic - Next topic

bx83

Is there a wonderful way of saying:

Code: ags
#define TIMER_BOMB 1
SetTimer(TIMER_BOMB,GetGameSpeed()*10*60);   //10 minutes
Display("You have %d:%d till bomb explodes",<get how much time in seconds is left from timer 1>);


This is all something I've pulled out of my arse, but you get what I mean. How do we get the current time-out for a timer? Or should I use the module timer, which will take me.... a long time.

Matti

You could have two variables, seconds an minutes. If seconds reach -1, set them to 59 and decrease the minutes by 1.

Alternatively you could make a while loop that substracts 60 from a number of seconds until there are less than 60 seconds left, while adding +1 to a minute variable. The first solution should be simpler though.

Khris

Here's how I'd do this:

Code: ags
int frames;

function room_Load() {
  frames = GetGameSpeed() * 60 * 10; // 10 minutes
}

function Splode() {
  Display("BOOM");
}

function room_RepExec() {
  if (frames == 0) Splode(); // timer runs out -> bomb explodes
  if (frames > -1) {  // if timer is not negative, update display
    int seconds = frames / GetGameSpeed();
    int minutes = seconds / 60;
    int timerSeconds = seconds % 60;
    lblTimer.Text = String.Format("You have %02d:%02d till bomb explodes", minutes, timerSeconds);
    frames--;  // count down until -1
  }
}

bx83

Khris, fantastic it works :)

Code: ags
function repeatedly_execute_always()
{
...

    //do sacebar coordinates
    if (ovCoordsVisible) {
      //BookTranslationTimer - number of game loops it takes to translate homer's book
      //PuppetTimer - number of game loops it takes to make current puppet
      
      //timer countdowns
      if (BookTranslationTimer>=0) BookTranslationTimer-=1;
      if (PuppetTimer>=0) PuppetTimer-=1;
      
      int GameSpeed=GetGameSpeed();
      
      int BSec=BookTranslationTimer/GameSpeed;
      int BMin=BSec/60;
      int BTimerSec=BSec%60;
      
      int PSec=PuppetTimer/GameSpeed;
      int PMin=PSec/60;
      int PTimerSec=PSec%60;
      
      if (ovCoords != null && ovCoords.Valid) ovCoords.Remove();
      ovCoords = Overlay.CreateTextual(50, 720, 1366, eFontNormal, 51520, 
      String.Format("room %d  mouse x%d,y%d   ft %d   tr %02d:%02d   pt %02d:%02d",
                     player.Room, mouse.x,mouse.y, FutureTime, BMin, BTimerSec,
                     PMin, PTimerSec)
                     );
    }
...
}


I just copied the timer (tics*min*sec etc.) to a var, starting reducing it by 1 in repeatedly_execute_always() if it was non-0.

SMF spam blocked by CleanTalk