Hello guest! Login or Register

How to use the System 'Wait' action

10 votes

Construct 2 r51 introduces a really handy new action: the System "wait" action. Here's how it works.

The Wait action waits a number of seconds before running the next action. It remembers all the picked objects so it works exactly like a normal event, just a bit later in time. During the wait, the rest of the event sheet 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.

For advanced users


The Wait action saves the state of the entire SOL and cancels the current event. When the wait time has expired, the entire SOL as it was is restored (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-SOL-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.

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!

Total of 2 edits. Last edit by Ashley on 22 August 2011 7:31 PM
Posted by

1
TheBen (812) Tuesday, August 23, 2011 at 10:56:11 PM
Useful, I suppose. Does everything in this tutorial apply to Construct Classic as well?
2
Ashley (48K) Wednesday, August 24, 2011 at 1:26:16 AM
Construct Classic doesn't have a Wait action, so no, not really.
1
danny (1,480) Thursday, August 25, 2011 at 8:59:33 PM
Nice addition Ashley!
1
gammabeam (8.4K) Sunday, September 04, 2011 at 5:10:56 PM
Just used it on a small game-test I am making.
Oh eM Gee! Great addition!
1
Wronghands (2,508) Monday, December 19, 2011 at 7:35:13 PM
I know it's the advanced section but it'll help if you put a link to somewhere that explains what you mean by "SOL"
0
Bigheti (7.8K) Friday, January 13, 2012 at 7:11:25 PM
Cool...and very interesting!
0
Geo (3.5K) Tuesday, March 06, 2012 at 9:48:11 PM
This is important for my game, I need to know: after Wait, can the event resume in the middle of running other events? I mean, let's say I call wait (1 second). In the meantime other events are processed. After exactly 1 second has elapsed, the system is in the middle of executing some other actions in another event. Will these actions in the other running event be interrupted?
0
Ashley (48K) Tuesday, March 06, 2012 at 10:48:57 PM
@Geo - no, it will always resume waiting events at the end of the event sheet. It will never skip from another event half way through, that would cause lots of weird bugs!
0
Geo (3.5K) Wednesday, March 07, 2012 at 7:15:19 AM
That is perfect! Thanks Ashley.
0
blackvikinggames (839) Monday, March 19, 2012 at 1:39:39 AM
Thank you very much!
0
Potato (1,050) Monday, April 09, 2012 at 3:53:26 AM
SOL = Selected Object List
in case someone wondering what it means when they read this tutorial.
1
joeykid6 (559) Monday, April 30, 2012 at 9:33:58 AM
I just learned that 'wait' actions will queue if their event runs more than once. This may seem obvious, but I thought it was worth mentioning. If you are using a 'wait' action in an event triggered by a variable, make sure to set the variable to something new before the wait kicks in, not after. Otherwise you'll get a queue of 60 waits for every second you're waiting.

You need to Login to post a comment!
Login to your account, or register a new account