Repeating events EASIER

Discussion in 'Mods & Lua Functions' started by Colandus, Apr 15, 2012.

  1. Colandus

    Colandus Support Team Support Team

    Joined:
    Jun 6, 2007
    Messages:
    2,424
    Likes Received:
    156
    Best Answers:
    18
    Code (Lua):
    1.  
    2. -----------------
    3. -- By Colandus --
    4.  
    5. _CONT = {}
    6. function continue(delay, n, ...)
    7.     local func = debug.getinfo(2).func
    8.     if(not _CONT[func]) then
    9.         _CONT[func] = true
    10.         addEvent(repeatContinue, delay, func, delay, n, ...)
    11.     end
    12. end
    13.  
    14. function repeatContinue(func, delay, n, ...)
    15.     if(n > 1) then
    16.         func(...)
    17.         addEvent(repeatContinue, delay, func, delay, n - 1, ...)
    18.     else
    19.         _CONT[func] = nil
    20.     end
    21. end
    22.  
    Then here is example of usage (talkaction):
    Code (Lua):
    1. function doThatShit(cid)
    2.     if(not isCreature(cid)) then
    3.         return
    4.     end
    5.    
    6.     doCreatureSay(cid, "HiHO", 1)
    7.  
    8.     -- repeat 4 times (will only repeat 4 times because it count the current call as 1!) each second
    9.     continue(1000, 5, cid)
    10. end
    11.  
    12. function onSay(cid, words, param, channel)
    13.     doThatShit(cid)
    14.     return true
    15. end
    16.  
    So all you have to do is to write continue(delay, repeatTimes, parameters) ! You don't need to write the function name, it will know that itself! You don't need any counter in your function, it will count itself!

    A normal script WITHOUT this function would look like this:
    Code (Lua):
    1. function doThatShit(cid, n)
    2.     if(not isCreature(cid)) then
    3.         return
    4.     end
    5.    
    6.     doCreatureSay(cid, "HiHO", 1)
    7.  
    8.     if(n > 1) then
    9.         addEvent(doThatShit, 1000, cid, n - 1)
    10.     end
    11. end
    12.  
    13. function onSay(cid, words, param, channel)
    14.     doThatShit(cid, 5)
    15.     return true
    16. end
    17.  
    Not the greatest difference but surely saves you some thinking and time.


    NOT WHAT YOU'RE LOOKING FOR?
    Check this out: http://otland.net/f163/sleep-not-beds-20193/
     
    Last edited: Dec 13, 2016
    abobakrwaheed74 likes this.
  2. owned

    owned Well-Known Member

    Joined:
    Nov 9, 2008
    Messages:
    1,897
    Likes Received:
    483
    Best Answers:
    0
    Thats awesome! Glad to see you back and releasing things for the community again.
     
  3. Colandus

    Colandus Support Team Support Team

    Joined:
    Jun 6, 2007
    Messages:
    2,424
    Likes Received:
    156
    Best Answers:
    18
    It's actually an old script but it was kinda hidden inside a tutorial so I had to make it its own thread!
     
  4. Mooosie

    Mooosie - Lua Scripter -

    Joined:
    Aug 2, 2008
    Messages:
    702
    Likes Received:
    27
    Best Answers:
    0
    Colandus, It looks like addEvent function! So I thought if I reload a script that is including your functions, does it cancel the function as it is doing in addEvent function? For example. I use addEvent function on a onUse script. Then when I reload actions, the addEvent suddenly cancels and doesn't count down the seconds. Does your functions do it too?
     
  5. Colandus

    Colandus Support Team Support Team

    Joined:
    Jun 6, 2007
    Messages:
    2,424
    Likes Received:
    156
    Best Answers:
    18
    Yes of course. You're not supposed to reload when your server is running as all variables are reset and events cancelled which will affect lots of scripts in your server.
     
    Cykotitan likes this.
  6. Colandus

    Colandus Support Team Support Team

    Joined:
    Jun 6, 2007
    Messages:
    2,424
    Likes Received:
    156
    Best Answers:
    18

Share This Page

Loading...