Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Kamileon on Mon 16/06/2014 08:25:08

Title: SOLVED: How to stop a timer
Post by: Kamileon on Mon 16/06/2014 08:25:08
Hey guys, here is my current dilemma: before my parrot flies into a building, he taps on the window and asks to come in. But because the timer doesn't stop, the speech keeps popping up, even if I tell cParrot to ChangeRoom. So how do I stop a timer? (been looking through past posts but they don't seem to cover randomisation as well).

Code (ags) Select

function room_Load()
{
  SetTimer(1, 120); //Timer for parrot tapping on window.
}

function room_RepExec() //This repeats the tapping on window for the parrot until its opened.
{
    int i;
    if (IsTimerExpired(1)) {
        i = Random(2);
        if (i == 0) cParrot.SayBackground("Hello.");
        else if (i == 1) cParrot.SayBackground("Tap tap tap.");
        else cParrot.SayBackground("Let me in.");
        SetTimer(1, 120);
    }
}
Title: Re: How to stop a timer
Post by: Khris on Mon 16/06/2014 08:45:30
The answer is a variable.

Code (ags) Select
bool parrot_action = true;

function room_RepExec() //This repeats the tapping on window for the parrot until its opened.
{
    int i;
    if (IsTimerExpired(1)) {
        ...
        if (parrot_action) SetTimer(1, 120);
    }
}

// somewhere else in the room script
  parrot_action = false;  // stop parrot
// OR restart
  parrot_action = true;
  SetTimer(1, 1);
Title: Re: How to stop a timer
Post by: Kamileon on Mon 16/06/2014 09:24:26
That script seems to work, but the parrot says the line once more (after it has left the room) before stopping indefinitely. Is there a way to hide or stop that one line (maybe even change that line to something like "goodbye")? Been trying to tinker with it but doesn't appear to be working for me.
Title: Re: How to stop a timer
Post by: Joe on Mon 16/06/2014 09:35:22
Maybe:

if (IsTimerExpired(1) && parrot_action) {
    ...
    SetTimer(1, 120);
}

in stead of:

if (IsTimerExpired(1)) {
    ...
    if (parrot_action) SetTimer(1, 120);
}
Title: Re: How to stop a timer
Post by: Kamileon on Mon 16/06/2014 12:26:32
Yes that worked! Thank you :grin: