lua:delayfunction
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
lua:delayfunction [2017/02/20 07:20] – 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. | ||
+ | |||
+ | Any number of arguments can be passed after the ' | ||
Example1: | Example1: | ||
Line 93: | Line 95: | ||
</ | </ | ||
- | Since areas are never destroyed, AREA: | + | Since areas are never destroyed, AREA: |
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: | ||
+ | |||
+ | <code lua> | ||
+ | function ankhSpeech(ch) | ||
+ | say(" | ||
+ | say(" | ||
+ | say(" | ||
+ | say(" | ||
+ | say(" | ||
+ | end | ||
+ | |||
+ | if ch: | ||
+ | emote(" | ||
+ | say(" | ||
+ | delay(2, ankhSpeech, nil, ch) | ||
+ | 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.1487575258.txt.gz · Last modified: 2017/02/20 07:20 by vodur