How to use the System 'Wait' action

6

Stats

21,363 visits, 33,261 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

The Wait action in the System object waits a number of seconds before running the next action. The Wait for signal action works very similarly, but instead of a time delay waits until you use a corresponding Signal action. They remember all the picked objects so it works exactly like a normal event, just a bit later in time. During the wait, other events continues to run as normal.

For example, consider:

+ On spacebar pressed

-> Player: spawn a bullet

-> Audio: play 'bang' sound

If you hit spacebar, your bullets emerge with a bang immediately. If you add a Wait action, though:

+ On spacebar pressed

-> System: wait 1 second

-> Player: spawn a bullet

-> Audio: play 'bang' sound

If you hit spacebar, there's a one second delay, then the bullet emerges with a bang. If you tap spacebar twice quickly, it queues up twice, so two bullets are shot, each one second after you hit spacebar.

You can also use the wait action multiple times in an event. For example:

+ On start of layout

-> Text: set text to "Hello there!" & newline

-> System: wait 1 second

-> Text: append text "How are you?" & newline

-> System: wait 1 second

-> Text: append text "Nice to meet you!"

This event adds each message to the text object at one second intervals. Don't forget the rest of your events are running as normal while the waits are waiting. Any events can set up as many waits as they want at any time, and it all runs as scheduled.

Here's another example:

+ Every 1 second

-> Sprite: set invisible

-> System: wait 0.3 seconds

-> Sprite: set visible

The sprite flashes.

One more example:

+ Every 2 seconds

-> Enemy: spawn 'bullet'

-> System: wait 0.2 seconds

-> Enemy: spawn 'bullet'

-> System: wait 0.2 seconds

-> Enemy: spawn 'bullet'

Every 2 seconds, the enemy fires a salvo of three bullets, each 0.2 seconds apart.

If you're feeling super pro, you can schedule events in loops too:

+ On start of layout

+ Repeat 10 times

-> System: wait loopindex seconds

-> Text: append text "Time = " & time & newline

This one event will cause a new line to be added to the text object every one second for ten seconds. This is because the "Repeat 10 times" condition runs the actions ten times, and each run schedules a wait of a different length of time followed by adding a line of text. This spreads all the "append text" actions out over time.

Note: if you wait for a constant amount of time in a loop, like this:

+ On start of layout

+ Repeat 10 times

-> Wait 1 second

-> Append text "Time = " & time & newline

This may not do what you expect. When the first 'Wait' is reached, the loop continues to run (since the rest of the event sheet carries on independently of waits). So the loop completes instantly, scheduling ten 'Append' actions each after a one second delay. So after one second, all ten actions run at the same time. If this isn't what you want, either use the Every X seconds system condition, or use the loopindex based variant above.

Note about using 'Wait' in functions

Note that in a function call, if you use 'Wait' then the function has finished by the time the next action after the 'Wait' runs. This means the function parameters are no longer available. You could work around this by copying function parameters to a local variable.

For advanced users

The Wait action saves which objects are picked and cancels the current event. When the wait time has expired or it is signalled, the picked objects are restored as they were when the wait started (minus any objects that got destroyed), and the event is resumed from the action following the wait action. It continues to run any subevents as well. At any point if another wait action is encountered, the same save-picked-objects-and-wait process happens, so you can make different parts of an event run at different times.

One more trick: "Wait 0 seconds" postpones the following actions until the end of the event sheet.

Waiting for a signal just waits until you use the Signal action instead of a time delay. For example if you have an event that does Signal "Enter" when the Enter key is pressed, then Wait for signal "Enter" will wait until you next press Enter. This provides a useful alternative way to schedule events beyond simply waiting for time delays: you can make events wait until a meaningful event happens in the game. Apart from that it works identically to the Wait action, including that other events keep running while it waits (which also applies to loops that carry on to the next iterations even though the current iteration started waiting). There can be multiple waits for one signal, and they all resume the next time the Signal action is used.

Conclusion

This is really powerful feature. If you ever used Construct Classic, you should find it a much more elegant solution than the Function object's "call after delay". Scheduling things over time by events has always been a bit of a headache, so hopefully this makes it nice and simple!

  • 2 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Hello!

    I would be very grateful if you could explain to me why examples below provides different results and how to make Wait action in the example#2 to work like in the example #1

    Thank you in advance for helping!

    Example #1

    Variables:

    x=0

    y=0

    z=0

    Condition:

    System|x=y

    Action:

    System|Add 1 to z

    System|Add 1 to x

    Result:

    x=1

    y=0

    z=1

    ----------------------------------

    Example #2

    Variables:

    x=0

    y=0

    z=0

    Condition:

    System|x=y

    Action:

    System|Add 1 to z

    System|Wait 3 seconds

    System|Add 1 to x

    Result:

    x=180

    y=0

    z=180

    • After hours of experiments I realized that much better to do it using Timer behavior (which I didn't know about before) for objects. Works much more predictable as for beginner.