Lua SLEEP implementation

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

  1. Colandus

    Colandus Support Team Support Team

    Joined:
    Jun 6, 2007
    Messages:
    2,424
    Likes Received:
    157
    Best Answers:
    18
    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 (Text):
    1.  
    2. sleep = coroutine.yield
    3.  
    4. function doSleep(co, ...)
    5.     if coroutine.status(co) ~= 'dead' then
    6.         local _, delay = coroutine.resume(co, ...)
    7.         addEvent(doSleep, delay, co)
    8.     end
    9. end
    10.  
    11. function enableSleep(f, ...)
    12.     if type(f) == 'function' then
    13.         local co = coroutine.create(f)
    14.         doSleep(co, ...)
    15.     end
    16. end
    17.  

    How to use? You need to do:
    Code (Text):
    1.  
    2. enableSleep(function()
    3.    -- CODE HERE and sleep will work :) Sleep dont work outside this!!
    4. end)
    5.  
    Example of talkaction script :p
    Code (Text):
    1.  
    2. function onSay(cid, words, param)
    3.     param = tonumber(param)
    4.     if param and param > 0 and param < 60 then
    5.         enableSleep(function()
    6.             doCreatureSay(cid, "Time for suicide!", TALKTYPE_ORANGE_1)
    7.             for i = 1, param do
    8.                 doCreatureSay(cid, "Suicide in " .. param - i .. " seconds!", TALKTYPE_ORANGE_1)
    9.                 sleep(1000) -- delay a second
    10.             end
    11.  
    12.             doCreatureSay(cid, "GOOD BYE :(", TALKTYPE_ORANGE_1)
    13.             doCreatureAddHealth(cid, -getCreatureHealth(cid))
    14.         end)
    15.     else
    16.         doPlayerSendCancel("Please enter a number between 1 and 59!")
    17.     end
    18. end
    19.  
    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:
    874
    Likes Received:
    64
    Best Answers:
    0
    First and reserved ~
    nicee, but didnt you told me you will stop releasing ^.-
    i did !
     
  3. Colandus

    Colandus Support Team Support Team

    Joined:
    Jun 6, 2007
    Messages:
    2,424
    Likes Received:
    157
    Best Answers:
    18
    i dont release scripts, only funny ones :D
     
  4. Jano

    Jano oturhaN

    Joined:
    Feb 21, 2008
    Messages:
    874
    Likes Received:
    64
    Best Answers:
    0
    :)
    this function works for npcs delays ? (=
     
  5. Colandus

    Colandus Support Team Support Team

    Joined:
    Jun 6, 2007
    Messages:
    2,424
    Likes Received:
    157
    Best Answers:
    18
    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
    Likes Received:
    0
    Best Answers:
    0
    Great script xD
     
    Last edited: Jan 28, 2009
  7. Colandus

    Colandus Support Team Support Team

    Joined:
    Jun 6, 2007
    Messages:
    2,424
    Likes Received:
    157
    Best Answers:
    18
    It's a function but thanks for bump :)
     
  8. Someonation

    Someonation New Member

    Joined:
    Dec 9, 2008
    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    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 Support Team Support Team

    Joined:
    Jun 6, 2007
    Messages:
    2,424
    Likes Received:
    157
    Best Answers:
    18
    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
    Likes Received:
    0
    Best Answers:
    0
    I want to make a handle that goes up... then sleep(200) and then goes down
     
  11. Colandus

    Colandus Support Team Support Team

    Joined:
    Jun 6, 2007
    Messages:
    2,424
    Likes Received:
    157
    Best Answers:
    18
    Non-movable items you should use its position ^.-

    If you mean a "lever", "switch" then you can just get the position.
    PHP:
    1.  
    2. function onUse(cid, item, frompos)
    3.     doTransformItem(item.uid, 1946)
    4.     enableSleep(function()
    5.         sleep(200)
    6.         doTransformItem(getTileItemById(frompos, 1946).uid, 1945)
    7.     end)
    8.  
    9.     return TRUE
    10.  
    I think it would work this way too (without sleep, as you only need to use it for a single function)
    PHP:
    1.  
    2. function onUse(cid, item, frompos)
    3.     doTransformItem(item.uid, 1946)
    4.     addEvent(doTransformItem, 200, getTileItemById(frompos, 1946).uid, 1945)
    5.  
    6.     return TRUE
    7.  
     
  12. Someonation

    Someonation New Member

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

    Roy Bgd New Member

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

    Someonation New Member

    Joined:
    Dec 9, 2008
    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    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,130
    Likes Received:
    1,025
    Best Answers:
    2
    @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,867
    Likes Received:
    13
    Best Answers:
    0
    Hahha very nice dude :p
     
  17. Exmortis

    Exmortis Member

    Joined:
    Jun 27, 2008
    Messages:
    189
    Likes Received:
    5
    Best Answers:
    0
    Exaktly what does this script do? :p
     
  18. Colandus

    Colandus Support Team Support Team

    Joined:
    Jun 6, 2007
    Messages:
    2,424
    Likes Received:
    157
    Best Answers:
    18
    maybe someone wanna have fun with this!!!!!
     
  19. Colandus

    Colandus Support Team Support Team

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

Share This Page

Loading...