Need help with a timer, alarm... something that pauses a movement

T

Teijal

Guest
Hi everyone,

I want to make my player move one upwards and then two downwards, but I want there to be a pause inbetween the two.
I tried running an alarm, waiting script... But nothing seemed to fit what kind of goal I had.
Does anyone have any tips? It would be extremely welcome!

Thank you in advance!
 

samspade

Member
Seeing code would be helpful as one up and two down doesn't mean a whole lot. One pixel? One step? One grid space? What's the unit of measurement?

For a delay you would want to use some type of timer. Depending upon what you want you might also want some type of state machine - e.g. have two states one for move up, one for move down where at the start of the state it moves and then there is a delay before switching to the next state.
 
T

Teijal

Guest
This is the code I have.

if (keyboard_check(ord("W"))) {
y-=1


if (step_delay>0)
{
step_delay -= 1;
}
else
if (keyboard_check(ord("W")))
{
{
y+=2
step_delay = 60;
}
}
if (keyboard_check(ord("A"))) {
x-=1
}
if (keyboard_check(ord("S"))) {
y+=1
}
if (keyboard_check(ord("D"))) {
x+=1
}

So, strictly speaking there are two possibilities: Using a timer or using a state. Which one would be best? Is using a loop also a possibility? which one is probably the best and least buggiest? I have a problem with the brackets currently, so I don't know whenever this would work or not. I'd like to know how the alarm function works because I can't seem to figure out how to activate it. Thank you for the patietience
Seeing code would be helpful as one up and two down doesn't mean a whole lot. One pixel? One step? One grid space? What's the unit of measurement?

For a delay you would want to use some type of timer. Depending upon what you want you might also want some type of state machine - e.g. have two states one for move up, one for move down where at the start of the state it moves and then there is a delay before switching to the next state.
I'm a total noob. Nice for helping me out.

Thank you
 

samspade

Member
You can't use a loop. Loops will repeat within the same frame and no other code will be run until it is done. So you would want to use a timer for this. You might actually want or need to use both a timer and state machine. Your code is simple enough that a single variable would be enough. I reworked the code to have a properly set up timer, and I think this is what you were going for, though giving how fast you allow movement in the other direction, you probably don't actually want a delay of 1 second (assuming roomspeed is 60) for up and down.

GML:
//changed this section
if (keyboard_check(ord("W"))) {

    //if holding W, count down
    step_delay -= 1;

    //this portion will 'fire' when the alarm reaches 0 or less
    if (step_delay <= 0)
    {
        //reset timer
        step_delay = 60;
      
        //move up or down based on current 'state'
        if (move_up)
        {
            y += 2;     
        }
        else
        {
            y -= 1;
        }
        //flip state so it will be the opposite next time
        move_up = !move_up;
      
    }
}
else
{
    //keep the timer at the default if W is not pressed
    step_delay = 60;
}

//unchanged
if (keyboard_check(ord("A"))) {
    x-=1
}

//unchanged
if (keyboard_check(ord("S"))) {
    y+=1
}

//unchanged
if (keyboard_check(ord("D"))) {
    x+=1
}
 
T

Teijal

Guest
You can't use a loop. Loops will repeat within the same frame and no other code will be run until it is done. So you would want to use a timer for this. You might actually want or need to use both a timer and state machine. Your code is simple enough that a single variable would be enough. I reworked the code to have a properly set up timer, and I think this is what you were going for, though giving how fast you allow movement in the other direction, you probably don't actually want a delay of 1 second (assuming roomspeed is 60) for up and down.

GML:
//changed this section
if (keyboard_check(ord("W"))) {

    //if holding W, count down
    step_delay -= 1;

    //this portion will 'fire' when the alarm reaches 0 or less
    if (step_delay <= 0)
    {
        //reset timer
        step_delay = 60;
     
        //move up or down based on current 'state'
        if (move_up)
        {
            y += 2;    
        }
        else
        {
            y -= 1;
        }
        //flip state so it will be the opposite next time
        move_up = !move_up;
     
    }
}
else
{
    //keep the timer at the default if W is not pressed
    step_delay = 60;
}

//unchanged
if (keyboard_check(ord("A"))) {
    x-=1
}

//unchanged
if (keyboard_check(ord("S"))) {
    y+=1
}

//unchanged
if (keyboard_check(ord("D"))) {
    x+=1
}
Hi, I tried running your script, I tweaked it a bit and tried several versions but it wouldn't work. I got this error:

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object RolkoietCalmWater:
Variable RolkoietCalmWater.move_up(100003, -2147483648) not set before reading it.
at gml_Object_RolkoietCalmWater_Step_0 (line 11) - if (move_up)
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_RolkoietCalmWater_Step_0 (line 11)

Then I tried making a variable but then it wouldn't run at all. I don't need it to be opposite though, the idea is that it's a current in a river and although you move upwards, you don't make any progress.
Do you have any ideas on what the problem might be?
 

samspade

Member
According to the error message you're using the variable move_up without initializing it first. (see below for more on reading error messages)

You'll have to initialize that variable first, most likely in the create event. However if the goal is to keep an instance relatively stationary, I'm not sure that this is the best way. As it will probably feel a little weird to push up, but then move backwards.

Instead you'd probably want to make the move up exactly like the other keypresses but clamp the min and max x and y values with the clamp() function.

If you wanted to get fancy you could move the boat back based on how far away it is, e.g. so you always add to the y value (moving it down) but the amount you add increases the farther away you go from the boat's starting y position. At some point then, you would reach an equilibrium where holding W would apply the exact same amount of force in one direction that would be applied in the opposite canceling each other out. But clamp might be easier to start with.


 
T

Teijal

Guest
I figures something out. Thanks for all the help, I'll take a look at your channel
 
Top