Lua SLEEP implementation

Discussion in 'Mods & Lua Functions' started by Colandus, Jan 17, 2009.

  1. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
    This is like addEvent, but easier :) It exit when receiving errors, so you don't need "if isPlayer(cid) == TRUE then return FALSE end" because it exit itself when errors :p

    TFS 0.3+ put in data/lib/functions.lua
    TFS 0.2 or other put in data/global.lua

    Code:
    sleep = coroutine.yield
    
    function doSleep(co, ...)
        if coroutine.status(co) ~= 'dead' then
            local _, delay = coroutine.resume(co, ...)
            addEvent(doSleep, delay, co)
        end
    end
    
    function enableSleep(f, ...)
        if type(f) == 'function' then
            local co = coroutine.create(f)
            doSleep(co, ...)
        end
    end
    

    How to use? You need to do:
    Code:
    enableSleep(function()
       -- CODE HERE and sleep will work :) Sleep dont work outside this!!
    end)
    
    Example of talkaction script :p
    Code:
    function onSay(cid, words, param)
        param = tonumber(param)
        if param and param > 0 and param < 60 then
            enableSleep(function()
                doCreatureSay(cid, "Time for suicide!", TALKTYPE_ORANGE_1)
                for i = 1, param do
                    doCreatureSay(cid, "Suicide in " .. param - i .. " seconds!", TALKTYPE_ORANGE_1)
                    sleep(1000) -- delay a second
                end
    
                doCreatureSay(cid, "GOOD BYE :(", TALKTYPE_ORANGE_1)
                doCreatureAddHealth(cid, -getCreatureHealth(cid))
            end)
        else
            doPlayerSendCancel("Please enter a number between 1 and 59!")
        end
    end
    
    If player logs out or dies, script will terminate... If you want something to happen if he logs out instead of just terminate you simply add "if isPlayer(cid) == FALSE then code_to_do_something end" (unfortunately you would need this each time a creature (or similar) function is called).


    Update: Added support for parameters.
     
    Last edited: Dec 18, 2016
    Forrest likes this.
  2. Jano

    Jano oturhaN

    Joined:
    Feb 21, 2008
    Messages:
    873
    First and reserved ~
    nicee, but didnt you told me you will stop releasing ^.-
    i did !
     
  3. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
    i dont release scripts, only funny ones :D
     
  4. Jano

    Jano oturhaN

    Joined:
    Feb 21, 2008
    Messages:
    873
    :)
    this function works for npcs delays ? (=
     
  5. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
    Nope, it's exactly same as addEvent but just that you write sleep and not addEvent :p NPC system is different ;>
     
  6. Someonation

    Someonation New Member

    Joined:
    Dec 9, 2008
    Messages:
    5
    Great script xD
     
    Last edited: Jan 28, 2009
  7. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
    It's a function but thanks for bump :)
     
  8. Someonation

    Someonation New Member

    Joined:
    Dec 9, 2008
    Messages:
    5
    just one think... when I use the "doTransformItem" there is a problem...
    it sais:
    and when I don't use it, it works... but I need to use it...
    so what do I do?
     
  9. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
    This is because OTServer has a UID system that will make the UID change into something else when using the item...

    You will not be able to fix this with sleep nor with addEvent. But you could always find workarounds somehow, what is the script you are trying to make?
     
    Last edited: Jan 29, 2009
  10. Someonation

    Someonation New Member

    Joined:
    Dec 9, 2008
    Messages:
    5
    I want to make a handle that goes up... then sleep(200) and then goes down
     
  11. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
    Non-movable items you should use its position ^.-

    If you mean a "lever", "switch" then you can just get the position.
    PHP:
    function onUse(ciditemfrompos)
        
    doTransformItem(item.uid1946)
        
    enableSleep(function()
            
    sleep(200)
            
    doTransformItem(getTileItemById(frompos1946).uid1945)
        
    end)

        return 
    TRUE
    end
    I think it would work this way too (without sleep, as you only need to use it for a single function)
    PHP:
    function onUse(ciditemfrompos)
        
    doTransformItem(item.uid1946)
        
    addEvent(doTransformItem200getTileItemById(frompos1946).uid1945)

        return 
    TRUE
    end
     
  12. Someonation

    Someonation New Member

    Joined:
    Dec 9, 2008
    Messages:
    5
    Thanks man... works great...
    and again... great function xD
     
  13. Roy Bgd

    Roy Bgd New Member

    Joined:
    Jan 31, 2009
    Messages:
    1
    ok ... GOOD im like you ; * <hah>
     
  14. Someonation

    Someonation New Member

    Joined:
    Dec 9, 2008
    Messages:
    5
    My script for a qusino (it has the sleep commend so I'm putting it here):
    have fun :D
     
  15. Summ

    Summ (\/)(;,,;)(\/) Y not? Staff Member Global Moderator

    Joined:
    Oct 15, 2008
    Messages:
    4,144
    @Colandus
    Best function ever made! LOVE YOU!
    It so useful.. No more needing to put addEvent for each delay.

    @up
    Why you need sleep() in this script. To me it seems senseless to put it there..
    And the English is bad.
     
  16. Sync

    Sync Ø,ø

    Joined:
    May 26, 2009
    Messages:
    1,868
    Hahha very nice dude :p
     
  17. Exmortis

    Exmortis Member

    Joined:
    Jun 27, 2008
    Messages:
    189
    Exaktly what does this script do? :p
     
  18. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
    maybe someone wanna have fun with this!!!!!
     
  19. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291

Share This Page