lua:delayfunction
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| lua:delayfunction [2014/12/27 07:21] – vodur | lua:delayfunction [2026/10/13 21:21] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | Syntax: delay( seconds[number], | ||
| - | |||
| - | 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. Example1: | ||
| - | |||
| - | <code lua> | ||
| - | goto(31404) | ||
| - | mdo(" | ||
| - | goto(10204) | ||
| - | end | ||
| - | |||
| - | delay(10, do_stuff) | ||
| - | </ | ||
| - | |||
| - | Example2: | ||
| - | |||
| - | <code lua> | ||
| - | delay(10, function() | ||
| - | goto(31404) | ||
| - | mdo(" | ||
| - | goto(10204) | ||
| - | end) | ||
| - | </ | ||
| - | |||
| - | Example3: | ||
| - | |||
| - | <code lua> | ||
| - | delay(10, function() | ||
| - | goto(31404) | ||
| - | mdo(" | ||
| - | goto(10204) | ||
| - | end, | ||
| - | " | ||
| - | </ | ||
| - | |||
| - | Notes: Remember that the delayed function will run in the env of the actor. Consider this example: | ||
| - | |||
| - | <code lua>-- let's say it's an mprog, ' | ||
| - | mob: | ||
| - | ch: | ||
| - | ch: | ||
| - | ch: | ||
| - | end) | ||
| - | </ | ||
| - | |||
| - | This will work fine, but would not run if ' | ||
| - | |||
| - | <code lua> | ||
| - | ch: | ||
| - | mob: | ||
| - | mob: | ||
| - | mob: | ||
| - | end) | ||
| - | </ | ||
| - | |||
| - | This one may not do what you expect. Since this will run in the env of ' | ||
| - | |||
| - | [[http:// | ||
| - | |||
| - | So when you pass the function to delay, if any of the references in the function match ' | ||
| - | |||
| - | If global variables need to be passed to the delayed function, they must first be declared locally as below: | ||
| - | |||
| - | <code lua> | ||
| - | ch: | ||
| - | lmob: | ||
| - | lmob: | ||
| - | lmob: | ||
| - | end) | ||
| - | </ | ||
| - | |||
| - | Exploding mob: | ||
| - | |||
| - | <code lua>-- EXPLODING MOB!!!! | ||
| - | -- ' | ||
| - | explode=explode or function() | ||
| - | echo(" | ||
| - | for k,v in pairs(mob.room.players) do | ||
| - | damage("' | ||
| - | end | ||
| - | 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 60 secs it will explode! | ||
| - | timerStarted=true | ||
| - | </ | ||
| - | |||
| - | 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 [[: | ||
lua/delayfunction.1419664900.txt.gz · Last modified: by vodur
