Repeating events EASIER

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

  1. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
    Code:
    -----------------
    -- By Colandus --
    
    _CONT = {}
    function continue(delay, n, ...)
        local func = debug.getinfo(2).func
        if(not _CONT[func]) then
            _CONT[func] = true
            addEvent(repeatContinue, delay, func, delay, n, ...)
        end
    end
    
    function repeatContinue(func, delay, n, ...)
        if(n > 1) then
            func(...)
            addEvent(repeatContinue, delay, func, delay, n - 1, ...)
        else
            _CONT[func] = nil
        end
    end
    
    Then here is example of usage (talkaction):
    Code:
    function doThatShit(cid)
        if(not isCreature(cid)) then
            return
        end
       
        doCreatureSay(cid, "HiHO", 1)
    
        -- repeat 4 times (will only repeat 4 times because it count the current call as 1!) each second
        continue(1000, 5, cid)
    end
    
    function onSay(cid, words, param, channel)
        doThatShit(cid)
        return true
    end
    
    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:
    function doThatShit(cid, n)
        if(not isCreature(cid)) then
            return
        end
       
        doCreatureSay(cid, "HiHO", 1)
    
        if(n > 1) then
            addEvent(doThatShit, 1000, cid, n - 1)
        end
    end
    
    function onSay(cid, words, param, channel)
        doThatShit(cid, 5)
        return true
    end
    
    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,896
    Thats awesome! Glad to see you back and releasing things for the community again.
     
  3. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
    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
    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 Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
    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 Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291

Share This Page