game-makergmlgame-maker-studio-2

Delay time in GMS2


I am trying to make it so that when you click it will show a different cursor_sprite for 0.25 seconds. I currently need some way to add a delay to this. Here is my code so far:

In create event:

/// @description Set cursor
cursor_sprite = spr_cursor;

In step event:

/// @description If click change cursor

if mouse_check_button_pressed(mb_left)
{
    cursor_sprite = spr_cursor2;
    // I want to add the delay here.
}

Solution

  • You could use the build-in Alarms for this, but I don't like these much when it becomes nested with parent objects.

    So instead of Alarms, this is the way I would do it:

    Create Event:

    cursor_sprite = spr_cursor;
    timer = 0;
    timermax = 0.25;
    

    I create 2 variables: the timer will be used to count down, and the timermax to reset it's time.

    Step Event:

    if (timer > 0)
    {
        timer -= 1/room_speed //decrease in seconds
    }
    else
    {
        cursor_sprite = spr_cursor;
    }
    
    if mouse_check_button_pressed(mb_left)
    {
        cursor_sprite = spr_cursor2;
        timer = timermax;
    }
    

    For each timer, I let it count down in the Step Event through 1/room_speed, that way, it will decrease the value in real-time seconds.

    Then you can set the timer through timer = timermax

    Then if the timer reaches zero, it'll do the given action afterwards.

    Though a reminder that it's in the Step Event, so once the timer reaches zero, it'll always reach the else statement if there are no other conditions before. Usually I use the else-statement to change conditions so it doesn't reach the timer code multiple times.