0

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!

schlime9k
  • 1
  • 1

1 Answers1

1

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.

Steven
  • 1,996
  • 3
  • 22
  • 33
  • I've done that, but it still isn't working properly, I've changed the timer value to 1 and it worked properly, so the timer IS subtracting something, but if the value of timer is anything over that, the `if` statement doesn't work at all. I do have this type of code in other parts of this project, I have one in a `switch state` case where I use another timer variable (named something more specific so I don't get confused) which actually works, I copied the premise but to work for this situation in particular, but I don't know why it stops after just 1 count. – schlime9k Jul 17 '23 at 15:08
  • I think that's because when it's set to 1, then `timer--` results into 0, which will pass for the if statement. It is possible that the timer sets back to a default value somewhere else, though that depends if it's still within the same object. I also recommend debugging so you know what value the timer had when it counts down, and before it enters the if-statement. – Steven Jul 18 '23 at 06:40