Tried attaching

Hi all,
I have set up a series of "run script after x seconds" in a cluster of rooms, which force the player to keep moving between rooms before the countdown expires in order to escape it each time. However, the countdown continues after players have left the room and it just stacks infinitely on top of any new ones activated.

I tried attaching an If player is in room requirement to the scripts, but it still continues running if they leave it does not make the script independent of that room. Is there any way I can shut down the scripts after the player leaves the room they are in, preferably with built in functions as opposed to manually written scripts.

If you store a reference to the timer, then you can cancel it later.

I assume that "run script after x seconds" will look like this in code view:

SetTimeout (50) {
  // some script goes here

If you want to be able to stop that timer before it's finished, you'd need to change it to:

game.current_timer = GetUniqueElementName("timer")
SetTimeoutID (50, game.current_timer) {
  // some script goes here

That generates a unique name (like "timer1" or "timer17"), names the timer, and stores the name in the attribute game.current_timer.

Then before you create a new timer (or any other time you want to cancel the timer), you can do:

if (HasString (game, "current_timer")) {
  if (not GetTimer (game.current_timer) = null) {
    destroy (game.current_timer)
    game.current_timer = null

That checks if the attribute contains a timer name, checks if the timer is still running, and then destroys it.

I'm not sure what this looks like in the GUI editor; but you can paste the code into code view and take a look yourself.

Alternatively, if you've got multiple timers that do the same thing, maybe you could just use a single timer. You would create a named timer when the player enters that cluster of rooms, and then instead of creating a new one as the player moves between rooms, just keep changing the interval.

This topic is now closed. Topics are closed after 60 days of inactivity.