- [[crpl:crplreference| CRPL reference]] <- [[crpl:crplreference#flow_control|Flow Control]]
===== Delay =====
^Arguments^Result^Notation^
|Time| |''n1 -- ''|
=== Description ===
The number of game loops to wait before allowing execution to pass to the next command. Timers used via SetTimer(0-3) continue to run and expire even while the script is delayed.
⚠️ Delay is subject to multiple bugs related to script execution. These bugs mostly surface when the game is saved and then loaded at a point where Delay is currently delaying a script. Depending on how it's used it can cause the game to crash or cause other issues. If possible it is recommended to not use Delay and instead rely on timers, such as [[GetTimer0]], [[SetTimer0]]
=== Examples ===
#Stop runtime of the script for 30 game ticks, equivalent of 1 second.
30 Delay
#Add 5 units of creeper at the current location.
CurrentCoords 5 AddCreeper
=== Bugs ===
== Calling Delay in a loop when the script has a defined :Awake function ==
do
I 2000 mod eq0 if
1 delay
endif
loop
...
The combination of a Do/Loop with a nested Delay and an Awake function... that turns out to be poison. A Delay stops execution and next frame will resume execution. If you save the game during a delay, the game saves the loop stack, and where execution should resume. When the game gets loaded, the problem is that the "Awake" function gets called and that processes the script. Part of this processing clears the loop stack. After Awake is processed, execution resumes after where the Delay left off. When the Loop command is executed it tries to look at the loop stack and low and behold finds it is empty. The way to work around it should be to not do that Delay inside a do/loop.
== Calling Delay from within a function ==
"BeforeDeath" Trace
@HandleDeath
"AfterDeath" Trace
<-Lives 1 sub ->Lives
<-Lives Trace
:HandleDeath
10 10 100 AddCreeper
10 Delay
10 10 100 AddCreeper
10 Delay
10 10 100 AddCreeper
10 Delay
In this scenario, there are delays defined in the HandleDeath function. If the game is saved and then loaded at the point where one of those delays is in effect (due to the function having been called earlier), the game will continue where it left off - at the `@HandleDeath` function call - but it will pick up **after it**. So the rest of the function will be skipped and the code will immediately continue to the "AfterDeath" stage. To avoid this, avoid delays in functions.