lua:delayfunction
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
lua:delayfunction [2014/12/27 07:17] – vodur | lua:delayfunction [2017/06/15 06:02] (current) – vodur | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | Syntax: delay( seconds[number], function[function] <, tag[string]>) | + | Syntax: delay( seconds<number>, func<function> [, tag<string>] [, …] ) |
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: | ||
+ | <code lua> | ||
function do_stuff() | function do_stuff() | ||
- | + | | |
- | goto(31404) | + | mdo(" |
- | + | goto(10204) | |
- | mdo(" | + | |
- | + | ||
- | goto(10204) | + | |
end | end | ||
delay(10, do_stuff) | delay(10, do_stuff) | ||
+ | </ | ||
Example2: | Example2: | ||
+ | <code lua> | ||
delay(10, function() | delay(10, function() | ||
- | + | | |
- | < | + | mdo(" |
- | | + | goto(10204) |
- | </ | + | end) |
- | + | ||
- | < | + | |
- | | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | | + | |
</ | </ | ||
Example3: | Example3: | ||
+ | <code lua> | ||
delay(10, function() | delay(10, function() | ||
- | + | | |
- | < | + | mdo(" |
- | | + | goto(10204) |
- | </ | + | end, |
- | + | " | |
- | < | + | |
- | | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | | + | |
</ | </ | ||
Notes: Remember that the delayed function will run in the env of the actor. Consider this example: | Notes: Remember that the delayed function will run in the env of the actor. Consider this example: | ||
- | – let's say it's an mprog, ' | + | <code lua> |
+ | -- let's say it's an mprog, ' | ||
mob: | mob: | ||
- | + | ch: | |
- | < | + | |
- | ch:goto(31404) | + | ch:goto(10204) |
+ | end) | ||
</ | </ | ||
- | < | + | This will work fine, but would not run if ' |
- | ch: | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | ch: | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | end) | + | |
- | </ | + | |
- | + | ||
- | This will work fine, but would not run if ' | + | |
- | + | ||
- | However: | + | |
+ | <code lua> | ||
ch: | ch: | ||
- | + | | |
- | < | + | mob: |
- | | + | mob: |
- | </ | + | end) |
- | + | ||
- | < | + | |
- | | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | | + | |
</ | </ | ||
Line 113: | Line 67: | ||
If global variables need to be passed to the delayed function, they must first be declared locally as below: | If global variables need to be passed to the delayed function, they must first be declared locally as below: | ||
- | local lmob=mob | + | <code lua> |
+ | local lmob=mob | ||
ch: | ch: | ||
- | + | lmob: | |
- | < | + | |
- | lmob:goto(31404) | + | lmob:goto(10204) |
+ | end) | ||
</ | </ | ||
- | < | + | Exploding mob: |
- | lmob:mdo(" | + | |
- | </ | + | |
- | < | + | < |
- | | + | -- EXPLODING MOB!!!! |
- | </ | + | -- ' |
+ | explode=explode or function() | ||
+ | echo(" | ||
+ | for k,v in pairs(mob.room.players) do | ||
+ | | ||
+ | end | ||
+ | mob:destroy() | ||
+ | end | ||
- | < | + | -- only start exploding once, it's all we need! |
- | end) | + | if timerStarted==true then return |
+ | delay(60, explode) -- If mob isn't dead within 60 secs it will explode! | ||
+ | timerStarted=true | ||
</ | </ | ||
- | Exploding mob: | + | Since areas are never destroyed, AREA:delay() can be used to ensure that the delayed function won't be canceled due to game object destruction. |
- | – EXPLODING MOB!!!! | + | 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** | ||
- | explode=explode or function() | + | As mentioned, you can pass arguments along to the delay function |
- | < | + | - 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. |
- | echo("The mob EXPLODES!" | + | - 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: |
- | for k,v in pairs(mob.room.players) do | + | |
- | </ | + | |
- | < | + | < |
- | | + | function ankhSpeech(ch) |
- | </ | + | say("Well %s, I suppose then that the time is near.", |
+ | | ||
+ | say(" | ||
+ | say(" | ||
+ | say(" | ||
+ | end | ||
- | < | + | if ch: |
+ | emote(" | ||
+ | say(" | ||
+ | delay(2, ankhSpeech, nil, ch) | ||
end | end | ||
</ | </ | ||
- | < | + | In above example, if the player logs out within the 2 second delay, |
- | mob: | + | |
- | </ | + | |
- | + | ||
- | end | + | |
- | + | ||
- | – only start exploding once, it's all we need! | + | |
- | + | ||
- | if timerStarted==true then return end | + | |
- | + | ||
- | delay(60, explode) – If mob isn't dead within | + | |
- | + | ||
- | timerStarted=true | + | |
- | + | ||
- | Since areas are never destroyed, AREA: | + | |
- | + | ||
- | The actual delay time may be up to 1 second | + | |
- | See also cancel | ||
lua/delayfunction.1419664657.txt.gz · Last modified: 2014/12/27 07:17 by vodur