User Tools

Site Tools


lua:delayfunction

This is an old revision of the document!


Syntax: delay( seconds[number], function[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.

Example1:

function do_stuff()
  goto(31404)
  mdo("dance")
  goto(10204)
end
 
delay(10, do_stuff)

Example2:

delay(10, function()
            goto(31404)
            mdo("dance")
            goto(10204)
          end)

Example3:

delay(10, function()
            goto(31404)
            mdo("dance")
            goto(10204)
          end,
          "blah") -- Can target cancel just this delay with cancel("blah")

Notes: Remember that the delayed function will run in the env of the actor. Consider this example:

-- let's say it's an mprog, 'ch' would be a prog argument
mob:delay(10, function()
            ch:goto(31404)
            ch:mdo("dance")
            ch:goto(10204)
          end)

This will work fine, but would not run if 'mob' was killed/destroyed during the 10 second delay. In 10 seconds the CH that was 'ch' in this script will goto 31404, dance, then goto 10204. However:

ch:delay(10, function()
            mob:goto(31404)
            mob:mdo("dance")
            mob:goto(10204)
          end)

This one may not do what you expect. Since this will run in the env of 'ch', this will have the original 'ch' mob goto 31404, dance, etc. This has to do with the way closures work in lua.

http://www.lua.org/pil/6.1.html "When a function is written enclosed in another function, it has full access to local variables from the enclosing function."

So when you pass the function to delay, if any of the references in the function match 'local' variables then those will be passed into the function, but anything else will basically be evaluated when the function is run ( in the respective env ). 'mob' is a global variable in the env, whereas the script arguments (such as 'ch') are actually local variables to the prog (which is a function).

If global variables need to be passed to the delayed function, they must first be declared locally as below:

local lmob=mob -- the original 'mob' var declared locally to the prog function
ch:delay(10, function()
            lmob:goto(31404)
            lmob:mdo("dance")
            lmob:goto(10204)
          end)

Exploding mob:

-- EXPLODING MOB!!!!
-- 'kill' trigger
explode=explode or function()
    echo("The mob EXPLODES!")
    for k,v in pairs(mob.room.players) do
        damage("'%s' 2000 3000 lethal", v.name)
    end
    mob:destroy()
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:delay() can be used to insure that the delayed function won't be cancelled due to game object destruction.

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 cancel

lua/delayfunction.1487287526.txt.gz · Last modified: 2017/02/16 23:25 by vodur