User Tools

Site Tools


lua:delayfunction

Differences

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

Link to this comparison view

Next revision
Previous revision
lua:delayfunction [2014/12/27 07:17] – created vodurlua: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 'tag' argument and these arguments will be passed to the delayed function when it is called. As with the case of the env owner being destroyed, if any of the extra args are game objects that have been destroyed during the delay, the function will simply not run.
  
 Example1: Example1:
  
 +<code lua>
 function do_stuff() function do_stuff()
- +  goto(31404) 
-goto(31404) +  mdo("dance"
- +  goto(10204)
-mdo("dance"+
- +
-goto(10204) +
 end end
  
 delay(10, do_stuff) delay(10, do_stuff)
 +</code>
  
 Example2: Example2:
  
 +<code lua>
 delay(10, function() delay(10, function()
- +            goto(31404) 
-          goto(31404) +            mdo("dance"
- +            goto(10204) 
-          mdo("dance"+          end) 
- +</code>
-          goto(10204) +
- +
-        end)+
  
 Example3: Example3:
  
 +<code lua>
 delay(10, function() delay(10, function()
- +            goto(31404) 
-          goto(31404) +            mdo("dance"
- +            goto(10204) 
-          mdo("dance"+          end, 
- +          "blah") -- Can target cancel just this delay with cancel("blah") 
-          goto(10204) +</code>
- +
-        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: 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, 'ch' would be a prog argument -- let's say it's an mprog, 'ch' would be a prog argument
- 
 mob:delay(10, function() mob:delay(10, function()
 +            ch:goto(31404)
 +            ch:mdo("dance")
 +            ch:goto(10204)
 +          end)
 +</code>
  
-          ch:goto(31404) +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: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:+
  
 +<code lua>
 ch:delay(10, function() ch:delay(10, function()
- +            mob:goto(31404) 
-          mob:goto(31404) +            mob:mdo("dance"
- +            mob:goto(10204) 
-          mob:mdo("dance"+          end) 
- +</code>
-          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. 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."+[[http://www.lua.org/pil/6.1.html|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). 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).
Line 79: 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:
  
 +<code lua>
 local lmob=mob -- the original 'mob' var declared locally to the prog function local lmob=mob -- the original 'mob' var declared locally to the prog function
- 
 ch:delay(10, function() ch:delay(10, function()
- +            lmob:goto(31404) 
-          lmob:goto(31404) +            lmob:mdo("dance"
- +            lmob:goto(10204) 
-          lmob:mdo("dance"+          end) 
- +</code>
-          lmob:goto(10204) +
- +
-        end)+
  
 Exploding mob: Exploding mob:
  
 +<code lua>
 -- EXPLODING MOB!!!! -- EXPLODING MOB!!!!
- 
 -- 'kill' trigger -- 'kill' trigger
- 
 explode=explode or function() 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
  
-  echo("The mob EXPLODES!")+-- 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 
 +</code>
  
-  for k,v in pairs(mob.room.players) do+Since areas are never destroyedAREA:delay() can be used to ensure that the delayed function won't be canceled due to game object destruction.
  
-      damage("'%s' 2000 3000 lethal"v.name)+The actual delay time may be up to 1 second longer than the value provided. So delay(0funcwill run func 0-1 seconds after executed. See also [[:lua:cancelfunction|cancel]]
  
-  end+\\ 
 +**Passing arguments**
  
-  mob:destroy()+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:
  
-end+  - 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.
  
--- only start exploding once, it's all we need!+Example 4:
  
-if timerStarted==true then return end+<code lua> 
 +function ankhSpeech(ch) 
 +  say("Well %s, I suppose then that the time is near.", ch.name) 
 +  say("You've kept your word, and I will keep mine. The last step to ridding"
 +  say("your body of the curse is to reincarnate you. Once you're ready, give"
 +  say("me the Ankh. I'll use the power of the Ankh to open a portal to Irena's"
 +  say("lair. Once you've slain her, I'll use the Ankh to reincarnate you."
 +end
  
-delay(60, explode-- If mob isn't dead within 60 secs it will explode! +if ch:carries(rvnum(5)) and ch:getval("r9_main_quest") == 6 then 
- +  emote("stares at you, completely stunned.") 
-timerStarted=true +  say("%s... the... the Ankh. You found it!"ch.name) 
- +  delay(2, ankhSpeech, nil, ch) 
-Since areas are never destroyedAREA:delay() can be used to insure that the delayed function won't be cancelled due to game object destruction.+end 
 +</code>
  
-The actual delay time may be up to 1 second longer than the value provided. So delay(0func) will run func 0-1 seconds after executed.+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.
  
-See also cancel 
  
lua/delayfunction.1419664635.txt.gz · Last modified: 2014/12/27 07:17 by vodur