lua:delayfunction
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
lua:delayfunction [2017/06/15 05:52] – vodur | lua:delayfunction [2017/06/15 06:02] (current) – vodur | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | Syntax: delay( seconds< | + | Syntax: delay( seconds< |
Begins a countdown to execute the provided function after the specified number of seconds. The provided function will run in the env of the actor, no matter what env the delay is invoked from. If the actor is destroyed by the time the function should run, it will simply not run. | Begins a countdown to execute the provided function after the specified number of seconds. The provided function will run in the env of the actor, no matter what env the delay is invoked from. If the actor is destroyed by the time the function should run, it will simply not run. | ||
Line 98: | Line 98: | ||
The actual delay time may be up to 1 second longer than the value provided. So delay(0, func) will run func 0-1 seconds after executed. See also [[: | The actual delay time may be up to 1 second longer than the value provided. So delay(0, func) will run func 0-1 seconds after executed. See also [[: | ||
+ | |||
+ | \\ | ||
+ | **Passing arguments** | ||
+ | |||
+ | As mentioned, you can pass arguments along to the delay function and these will in turn be passed to the delayed function when it it called. While it's possible to avoid using arguments by using closures, it is better practice to pass arguments for 2 reasons: | ||
+ | |||
+ | - Per notes above about closures, it's not always obvious which variables you are referencing are local to the outer scope (creating a closure) and which are global (no closure). Having your delayed function take arguments and explicitly passing arguments to the delay function is much more clear. | ||
+ | - Any argument passed in this way will be checked if it's been invalidated (destroyed) before the delayed function runs. The function will not execute if any of the passed arguments are destroyed. | ||
Example 4: | Example 4: | ||
<code lua> | <code lua> | ||
- | |||
function ankhSpeech(ch) | function ankhSpeech(ch) | ||
say(" | say(" | ||
Line 117: | Line 124: | ||
end | end | ||
</ | </ | ||
+ | |||
+ | In above example, if the player logs out within the 2 second delay, the ankhSpeech will simply not run, rather than running and having a script error for referencing a ' | ||
+ | |||
lua/delayfunction.1497505920.txt.gz · Last modified: 2017/06/15 05:52 by vodur