User Tools

Site Tools


lua:delayfunction

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
lua:delayfunction [2017/06/14 22:52]
vodur
lua:delayfunction [2017/06/14 23:02] (current)
vodur
Line 1: Line 1:
-Syntax: delay( seconds<​number>,​ func<​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.
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 [[:​lua:​cancelfunction|cancel]] 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:​cancelfunction|cancel]]
 +
 +\\
 +**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("​Well %s, I suppose then that the time is near.",​ ch.name)   say("​Well %s, I suppose then that the time is near.",​ ch.name)
Line 117: Line 124:
 end end
 </​code>​ </​code>​
 +
 +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 '​ch'​ which points to a destroyed CH.
 +
  
lua/delayfunction.txt · Last modified: 2017/06/14 23:02 by vodur