schlime9k
schlime9k

Reputation: 1

Timer variable not counting down in GameMaker Studio 2

I want to add a little timer so there's a delay when warping between rooms so a sprite animation can play for the player, but for some reason my timer variable isn't working and I don't know what's wrong with it.

Here's the code, this is in a collision event between the player and warp object

    timer = 17;
    sprite_index = spr_playerenter;
    image_speed = anim_speed;
    state = "CANT_MOVE";
    
    timer--;

    if (timer <= 0) {
    room_goto(other.targetRoom);
    x = other.targetX;
    y = other.targetY;
    state = "IDLE";
    }

With the timer, I made the variable and added timer--; because that's what is needed to have the value decrease, right? And the rest is pretty simple, when the timer reaches 0, the player will be warped to the room stated as the variable at this x and y position.

The sprite animation did play, but I guess the timer isn't working because nothing stated in the timer happened at all.

Any help is appreciated, thanks!

Upvotes: 0

Views: 162

Answers (1)

Steven
Steven

Reputation: 2122

From how it currently looks like, you're counting down the timer at the same place where it's defined.

Assuming everything is currently placed in the Step Event, you should move the timer = 17 (probably along with everything until state = "CANT_MOVE";) to the Create Event.

The Create Event will only be called once during creation of the object, where the Step Event is always looping and checking. That causes the timer to reset back to 17. This is why defined variables are better placed in the Create Event.

Upvotes: 1

Related Questions