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.
Well, you could use this (you'll need some GUI [in this case gCD] and a label [in this case lblCTD])
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
}
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
Uh, I hate dumbing stuff down. Do you want the code a little bit easier or shall I clarify what to put where?
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
It mostly already tells you that in Akatosh's code, but here it is in more detail:
int countdown;
bool countingDown;
This goes in the Global Script, at the very top.
// 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 (http://americangirlscouts.org/agswiki/Scripting%2C_Code_%26_Interaction#Working_with_variables:_the_basics), as well as various places around the forums.
//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:
//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
}
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
How can I set the time on 15 minutes and let the GUI display timer something like this 15:00
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 (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=17017.msg209600#msg209600)) replace:
string buffer;
StrFormat(buffer, "%02d:%02d", mins, secs);
SetLabelText(YOURGUIHERE, YOURLABELNUMBERHERE, buffer);
With:
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 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.
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?
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".
If i want, for example, changeroom when the timer is set on 9:00, how i do?
I used:
if (countdown == 540) {
Changeroom 2;
}
But don't works.
I've use 540 because i think in seconds.
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?
Ok, i had forgotten of the loops.... ;)
Now, go!
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.
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
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 :)
Show us the code!! (There should be a smiley for that phrase.)
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?
Uh, GUIs are always displayed on top of everything else.
If the bomb is an object, you'll have to use another object to display to countdown.
If it isn't, you can use RawPrint.
I have used this code in my game but there is a problem it only shows a 9 second countdown how do i increase that
Well the first code snippet being this:
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
}
Offers a 10 second countdown. I guess that's the one you've used. It says right in the comments where it needs to be changed to increase the time though. The line "countdown = 400;" controls how long to run the countdown for. Changing just that we could do instead:
int countdown;
bool countingDown;
// where you want the countdown to be triggered
countdown = 60 * GetGameSpeed(); // length in seconds * game speed
countingDown=true;
gCd.Visible=true;
//in repeatedly_execute
if (countingDown == true) {
countdown--;
lblCTD.Text=String.Format("Time remaining: %d seconds", countdown / GetGameSpeed());
if (countdown <= 0) player.ChangeRoom(999); //the game over room
}
I replaced the instances of 40 with GetGameSpeed() above just in case you have a setting that will change the game speed from the default of 40 FPS. Beyond that I only changed the aforementioned line so that it now runs for 60 seconds instead of just 10. If you wanted to do minutes and seconds you could do:
int mins = 5;
int secs = 40; // replace with whatever time you want
int countdown = ((mins * 60) + secs) * GetGameSpeed();
// ...in rep_ex
mins = (countdown / GetGameSpeed()) / 60;
secs = (countdown / GetGameSpeed()) - (mins * 60);
Thanks,
got the timer working the way I wanted
one question how do i stop the timer when im done with it if they do the desired action before the countdown is completed
and how would I restart the timer if i need them to do a series of actions that would restart the timer with each action
To disable the timer entirely you could change this line in rep_ex:
if (countdown <= 0) player.ChangeRoom(999);
To something like:
if (countdown == 0) player.ChangeRoom(999);
Then to disable the countdown you could just simply put:
countdown = -1;
And finally in rep_ex, before the other code:
gCd.Visible = (countdown > 0); // show or hide gCd based on the value of countdown
That way all you have to do is set the countdown timer to whatever you want it to be each time you need the action to start. Here's a revised version of the code:
int countdown = -1; // we'll use this as both our timer and our counting down flag
// where you want the countdown to be triggered
countdown = 60 * GetGameSpeed(); // length in seconds * game speed
//in repeatedly_execute
gCd.Visible = (countdown > 0);
if (countdown > 0) {
countdown--;
lblCTD.Text = String.Format("Time remaining: %d seconds", countdown / GetGameSpeed());
if (countdown == 0) player.ChangeRoom(999); //the game over room
}
// disable the countdown
countdown = -1;
Edit: Nothing like a bit of self-promotion! :=
You can now check out the CountDown module (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=39606.msg521521) to simplify the process! :)
Im using this script or a mod version with the timer being the amount of time I need for my game
I need to add five seconds to this timer every time a character does a specific act how would I do that?
If you're using the code that I just posted then you would just have to do:
// player triggers checkpoint
countdown += (5 * GetGameSpeed());