Countdown timer GUI

Started by , Sun 04/03/2007 13:55:00

Previous topic - Next topic

neilb4god

Does anyone know how to make a simple countdown timer in a GUI?

There was an AGS game called 'Abducted' a while back that featured one.

All I want to do to have a visible timer that sends the player to the 'game over room' when it reaches zero.

Akatosh

Well, you could use this (you'll need some GUI [in this case gCD] and a label [in this case lblCTD])

Code: ags

int countdown; 
bool countingDown;

// where you want the countdown to be triggered
countdown=400;//length in seconds*40
countingDown=true;
gCd.Visible=true;

//in repeatedly_execute
if (countingDown==true) {
countdown--;
lblCTD.Text=String.Format("Time remaining: %d seconds", FloatToInt(IntToFloat(countdown/40)));
if (countdown<=0) player.ChangeRoom(999); //the game over room
}

neilb4god

Thanks very much for that.

Unforunately, I'm not au fait with scripting. Would it be an imposition to ask you to dumb it down a bit so as I can apply it? I am familiar with all the other (non-scripting) elements of AGS.

Thanks again

Akatosh

Uh, I hate dumbing stuff down. Do you want the code a little bit easier or shall I clarify what to put where?

neilb4god

First of all I need to know where to put the script you posted. Does it go in global script? If so, where in global script? Having not yet dabbled in scripting that's the only bit of script I know so far.

If I can get something basic working I'll probably be able to figure the rest out myself.

Thanks

Ashen

It mostly already tells you that in Akatosh's code, but here it is in more detail:
Code: ags

int countdown;
bool countingDown;

This goes in the Global Script, at the very top.

Code: ags

// where you want the countdown to be triggered
countdown=400;//length in seconds*40
countingDown=true;
gCd.Visible=true;

This goes wherever you want to start the countdown - this could be a Character interaction, the start of the game (the game_start function), after an introductary cutscene ... wherever. However, if it's going to be started in a Room script (e.g. an Object or Hotspot interaction), you'll need to make countdown and countingDown global variables - explained in the BFAQ, as well as various places around the forums.

Code: ags

//in repeatedly_execute
if (countingDown==true) {
countdown--;
lblCTD.Text=String.Format("Time remaining: %d seconds", FloatToInt(IntToFloat(countdown/40)));
if (countdown<=0) player.ChangeRoom(999); //the game over room
}

Again goes in the Global Script, in repeatedly_execute.


For me, I'd probaly leave out the countingDown varible - it's not really needed:
Code: ags

//Top of Global Script
int countdown;

// where you want the countdown to be triggered
countdown=400;//length in seconds*40
gCd.Visible=true;

//in repeatedly_execute
if (countdown > 0) {
  countdown--;
  lblCTD.Text=String.Format("Time remaining: %d seconds", FloatToInt(IntToFloat(countdown/40)));
}
else if (countdown == 0) {
  player.ChangeRoom(999); //the game over room
  countdown = -1; // So 'ChangeRoom' isn't run repeatedly
}
I know what you're thinking ... Don't think that.

strazer

Sorry for the slight bump but I've stumbled upon the thread where we discussed the countdown clock in "Abducted": http://www.adventuregamestudio.co.uk/yabb/index.php?topic=17017

Meystari F

How can I set the time on 15 minutes and let the GUI display timer something like this  15:00

Ashen

#8
Read the thread strazer linked to, it deals with a Min:Sec format display. You'll just need to update the string handling to use Strings, and OO-style commands. Basically (from this code) replace:
Code: ags

string buffer;
StrFormat(buffer, "%02d:%02d", mins, secs);
SetLabelText(YOURGUIHERE, YOURLABELNUMBERHERE, buffer);


With:
Code: ags

lblCountdown.Text=String.Format("%d:%d", mins, secs); // Or whatever your Label is called


EDIT:
Corrected typo in updated code. (That's what I get for cut-and-pasting my code together...) Thanks, Fribbi.
I know what you're thinking ... Don't think that.

Meystari F

I found a error in this code but don't worry I saw what the error was. So this is the right code.

lblCountdown.Text=String.Format("%d:%d", mins, secs); // Or whatever your Label is called

And many thanks for the help.

Tequila144

I've created the gui (cd)... I've called the label0 "CTD"... but when i test the game, the error it is: "Undifined token 'lblCTD' ".. 
Why?

Khris

That's a bit misleading in the manual: there's no automatic "lbl" prefix for labels, unlike e.g. the "h" for hotspots.
Either call the label "lblCTD" or reference it by just "CTD".

Tequila144

If i want, for example, changeroom when the timer is set on 9:00, how i do? 
 
I used:

Code: ags

if (countdown == 540) {
Changeroom 2;
}



But don't works.
I've use 540 because i think in seconds.

OneDollar

Its a little off topic so sorry for that, but I was curious about why all the answers (including the other linked topic) use integers that are changed every game loop instead of timers? Are there any problems with timers, or issues you might run in to using them that I'm not aware of?

Just wondering

And Tequila, because the timer is counting down game loops (approximately 40 a second, your 9 minutes is actually 540 seconds multiplied by 40 loops a second which is 21600. Your code would be waiting for the countdown to hit 13.5 seconds (540 loops/40 loops a second = 13.5 seconds).

Oh, and have you made sure its in repeatedly_execute?

Tequila144

Ok, i had forgotten of the loops.... ;)
Now, go!

monkey0506

Quote from: OneDollar on Mon 21/01/2008 23:37:19Its a little off topic so sorry for that, but I was curious about why all the answers (including the other linked topic) use integers that are changed every game loop instead of timers? Are there any problems with timers, or issues you might run in to using them that I'm not aware of?

Most of us Scriptogrammers find the usage of the built-in timers to be foolish, childlike, and deprecated. We create our own which gives us better control of when/how often they are updated, if/when they might be reset, etc. and so forth. As such, we attempt to force this belief down the throats of all those around us as well. :=

On a more serious note, it's really a matter of preference. Using an integer can give you better control though, so for many it's the obvious choice. There's nothing actually wrong with using the built-in timer system.

OneDollar

Ok, a case of "I'm going to do it my way because I can"? That makes sense. I guess using integers gives you split second control. I'll make a mental note not to use timers in case I become frowned upon by the AGS elite... ;D

proskiier

#17
How do i make it so that it only comes up with the GUI when you click a certain object in a certain room


right now it is popping up no matter what room I am in, and sending me to my death room(as planned) but right away :(

help please :)

Khris

Show us the code!! (There should be a smiley for that phrase.)

proskiier

ok i fixed everything and my timer is actually the timer on a bomb.... everything works fine exactly how i want it to except

when my character walks in front of the timer i want it to be behind him, and right now it shows up on his body, so it ticks through his body.

anyone know anything I can do to fix it?

SMF spam blocked by CleanTalk