• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code Wait function in game maker

  • Thread starter Anthony Summers
  • Start date
A

Anthony Summers

Guest
I've been researching so I know about the wait function but this code below isnt working.
I'm trying to make it wait 1 second before going through the outer loop again.

Essentially this is what I have:

for(){
...
for(){
...
}
alarm[0] = 1* 30;

}

Thanks for the help
 
Don't know exactly what you want to do, or what the heck your code is supposed to do.

But here's a GM forum thread with nearly exactly the same title as yours: https://forum.yoyogames.com/index.php?threads/wait-function.40419/ I'm going to add that you probably don't want an actual wait function, which would pause the entire game (basically frame lock it all). Assuming you just want to not do something for a few frames then this thread will be fine.
 
I

icuurd12b42

Guest
Code:
///wait(microsecs)
var tend = get_timer()+argument1;
while(get_timer()<tend)
{
    //do nothing
}

for(){
...
for(){
...
}
wait(1 * 1000000); //wait 1 second

}
not recommended that you stall the game like that but it's useful when debugging
 
There you go, that's a better answer if you do indeed want to stall your game. For that I usually prefer breakpoints in the debugger though.
 
A

Anthony Summers

Guest
There you go, that's a better answer if you do indeed want to stall your game. For that I usually prefer breakpoints in the debugger though.
if you want me to describe the logic I am doing. I am making a character move by 32 pixels by doing obj.x+=32. than making it wait, than ill do another obj.x += 32 after a certain amount of time has passed. Think of an ai for a game like chess, as its a strategy game.

As this code is a for loop for all my objects, currently they all move instantly to the correct point. I'd like them to

1) move slowly to the current point and
2) move one at a time
 
Last edited by a moderator:

Fixer90

Member
I just use my own script function activates an alarm event in seconds:
Code:
alarm[argument1] = argument0 * room_speed;
I call it scr_wait. You input how many seconds to wait, and which alarm event to go to. For example, if you wanted to go to the Alarm 0 event after 2 seconds, you'd just say
Code:
scr_wait(2, 0);
 

FrostyCat

Redemption Seeker
I suggest that you read this two articles:
In particular, you need to give up this this mindset that automatically equates all repetition with loops. Repetition over time should always rely on repeated step checks, alarms or timelines in GM, never on a closed loop with a blocking wait. The blocking wait loop mindset had a place in single-tasking systems before the 1990's, but no place in multi-tasking systems in 2017.
When NOT to Use Loops

NEVER check a condition in loop form if you answer "yes" to any of the following:

- Do the actions within need to repeat gradually over time in order to be meaningful?
- Do you need other background processes to continue running while it is repeating? (e.g. speed-based movement, alarms/countdowns, user input, networking packets)

- Do the actions that work toward the stopping condition lie outside the block?
- Does the repeating condition rely on a background process to work properly?
- Does the repeating condition involve real-time user input? (e.g. keyboard, mouse, gamepad and touch screen presses)
Since this involves a series of jobs done over time, I suggest that you use a worker object with a job queue like this:

Create:
Code:
queue = ds_queue_create();
moving_instance = noone;
target_x = -1;
target_y = -1;
flag_delay = 0;
step_size = 32;
step_delay = room_speed;
Cleanup:
Code:
ds_queue_destroy(queue);
Step:
Code:
// Try to pull a job from the queue if idle
if (moving_instance == noone && !ds_queue_empty(queue)) {
  if (!ds_queue_empty(queue)) {
    var job = ds_queue_dequeue(queue);
    moving_instance = job[0];
    target_x = job[1];
    target_y = job[2];
    flag_delay = 0;
  }
}
// If busy with a job, move the job's working instance. Use a rate-capping flag to space apart moves.
else {
  if (flag_delay <= 0) {
    if (point_distance(moving_instance.x, moving_instance.y, target_x, target_y) <= step_size) {
      moving_instance.x = target_x;
      moving_instance.y = target_y;
      moving_instance = noone;
    } else {
      with (moving_instance) {
        mp_linear_step(other.target_x, other.target_y, other.step_size, false);
      }
    }
    flag_delay += step_delay;
  } else {
    flag_delay--;
  }
}
Then you simply push tasks onto its queue and leave the worker to do its work over its step cycle:
Code:
/// @function queue_move(instance, x, y)
/// @description Move an instance to (x, y) asynchronously with delays between each move, creating the worker if it does not already exist.
/// @param {real} instance The ID of the instance to move.
/// @param {real} x Target x coordinate.
/// @param {real} y Target y coordinate.
{
if (!instance_exists(obj_worker)) {
  instance_create_layer(argument1, argument2, "instance_layer", obj_worker);
}
with (obj_worker) {
  ds_queue_enqueue(queue, [argument0, argument1, argument2]);
}
}
 
Top