Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: aussie on Sat 02/10/2004 12:45:22

Title: Countdown clock
Post by: aussie on Sat 02/10/2004 12:45:22
I'd like my game to take place in ten minutes. If the player can't finish it within that period of time, the game ends and he loses.

For that, I'd like to have a countdown clock in the upper right corner of the screen, so the player knows eactly how long there's left.

I know clocks have been spoken of before, but I'm not sure if this has been done. Ideas?

Title: Re: Countdown clock
Post by: strazer on Sat 02/10/2004 13:06:56
Try this:

// main global script

int gameloops=0;

function repeatedly_execute() {
//...

gameloops++;
int mins = ((gameloops / GetGameSpeed()) / 60) % 60;
if (mins == 10) {
Display("You lose!");
RestartGame();
}

//...
}
Title: Re: Countdown clock
Post by: on Sat 02/10/2004 14:48:51
Thanks strazer, that works pretty well. I thinks I can get the second working from there.

BTW, how could I get the clock permanently on the screen?
Title: Re: Countdown clock
Post by: strazer on Sat 02/10/2004 14:56:16
Hm, that's actually not the countdown clock you were asking for...

Then how about this:

// main global script

int countdown=-1; // countdown initally off


function repeatedly_execute_always() {
// runs even when game is blocked

//...

if (countdown > -1) countdown--; // if countdown enabled, decrease it
int mins = ((countdown / GetGameSpeed()) / 60) % 60;
int secs = (countdown / GetGameSpeed()) % 60;
string buffer;
StrFormat(buffer, "%02d:%02d", mins, secs);
SetLabelText(YOURGUIHERE, YOURLABELNUMBERHERE, buffer);

//...
}


function repeatedly_execute() {
//...

if (countdown == 0) { // countdown expired
Display("You lose!"); // not allowed in rep_ex_always
RestartGame();
}

//...
}


export countdown;


// room script file

import int countdown;


function room_a() {
  // script for room: First time player enters screen
//...

  countdown = (10*60) * GetGameSpeed(); // start countdown (ten minutes)

//...
}
Title: Re: Countdown clock
Post by: on Sat 02/10/2004 16:19:30
YOU GURU!!!  ;D

That works perfect.

One last question:

What if I want to start the clock right after the intro (as it is, the coundown would be starting as the intro begins, right?);

If that works I'm gonna have to put your name in the credits in big flashy golden characters!!!
Title: Re: Countdown clock
Post by: strazer on Sat 02/10/2004 17:29:36
QuoteWhat if I want to start the clock right after the intro (as it is, the coundown would be starting as the intro begins, right?)

True.
I've finally put the code into a testgame and updated my post above. Should work now.
Move
  countdown = (10*60) * GetGameSpeed(); // start countdown (ten minutes)
to the "Player enters screen (after fadein)" script of the room that follows the intro.

QuoteIf that works I'm gonna have to put your name in the credits in big flashy golden characters!!!

Hehe, you don't have to do that. I'm just glad I could help. :)
Title: Re: Countdown clock
Post by: on Sat 02/10/2004 19:06:27
Quote from: strazer on Sat 02/10/2004 17:29:36
QuoteWhat if I want to start the clock right after the intro (as it is, the coundown would be starting as the intro begins, right?)

True.
I've finally put the code into a testgame and updated my post above. Should work now.
Move
Ã,  countdown = (10*60) * GetGameSpeed(); // start countdown (ten minutes)
to the "Player enters screen (after fadein)" script of the room that follows the intro.

QuoteIf that works I'm gonna have to put your name in the credits in big flashy golden characters!!!

Hehe, you don't have to do that. I'm just glad I could help. :)


Nah, that didn't quite do the trick...

1. I get a message saying "undefined token: countdown". I think it's because the room scripts are not in the global script (only characters and dialogs are, I think).

2. Still, if I put it under "Player enters screen (after fadein)", the clock should start every time I get into the screen, shouldn't it? I would probably have to be in "First time player enters screen", if anything.

Title: Re: Countdown clock
Post by: strazer on Sat 02/10/2004 19:12:17
1. Hence the "export" and "import" lines above.

2. You're right!
Title: Re: Countdown clock
Post by: on Sat 02/10/2004 20:20:46
Oh, of course... the old import and export statements!  :-\

How could I forget...

You're a champ straz. It works perfect now.

Title: Re: Countdown clock
Post by: strazer on Wed 06/10/2004 12:24:30
It recently dawned on me that there could be a problem:

Repeatedly_Execute_Always runs even when the game is blocked, Repeatedly_Execute does not.
Therefore, Rep_Ex won't notice the expiration (countdown == 0) when the countdown expires during a blocking scene since Rep_Ex_Always decreases it until it's -1.

So you might have to use another variable after all (iscountdownexpired or something), use Timers or put everything in Rep_Ex.

I don't want to edit the code again, so keep this in mind and change it yourself.
Title: Re: Countdown clock
Post by: aussie on Wed 06/10/2004 12:32:48
In any case, I have noted that the clock does stop when a message pops up, for example. Not that it bothers me. In fat, it works quite well and I'm a fair way along the game now.

I'll just try to put everything in Rep_Ex_Always, see how it goes.
Title: Re: Countdown clock
Post by: on Wed 06/10/2004 16:45:38
It's fixed and it works now. Only that the game waits for the blocking action to finish in order to trigger the required event.

But that's ok.