• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua SLEEP implementation

Colandus

Advanced OT User
Senator
Joined
Jun 6, 2007
Messages
2,434
Solutions
19
Reaction score
218
Location
Sweden
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:
First and reserved ~
nicee, but didnt you told me you will stop releasing ^.-
i did !
 
Nope, it's exactly same as addEvent but just that you write sleep and not addEvent :p NPC system is different ;>
 
just one think... when I use the "doTransformItem" there is a problem...
it sais:
luaDoTransformItem(). Item not found

and when I don't use it, it works... but I need to use it...
so what do I do?
 
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:
Non-movable items you should use its position ^.-

If you mean a "lever", "switch" then you can just get the position.
PHP:
function onUse(cid, item, frompos)
    doTransformItem(item.uid, 1946)
    enableSleep(function()
        sleep(200)
        doTransformItem(getTileItemById(frompos, 1946).uid, 1945)
    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(cid, item, frompos)
    doTransformItem(item.uid, 1946)
    addEvent(doTransformItem, 200, getTileItemById(frompos, 1946).uid, 1945)

    return TRUE
end
 
My script for a qusino (it has the sleep commend so I'm putting it here):
function onUse(cid, item, frompos, item2, topos)
if item.itemid == 9901 and doPlayerRemoveMoney(cid,10000) == 1 then
enableSleep(function()
doTransformItem(item.uid,item.itemid+1)
lucky = math.random(1, 5)
if lucky == 1 then
sleep(500)
doPlayerAddItem(cid,2160,2)
doSendMagicEffect(getPlayerPosition(cid), 28)
doPlayerSendTextMessage(cid, 21, "GZ...You just dubbeled your bet")
elseif lucky == 2 then
sleep(500)
doPlayerAddItem(cid,2160,3)
doSendMagicEffect(getPlayerPosition(cid), 29)
doPlayerSendTextMessage(cid, 21, "GZ...You just tripeled your bet")
elseif lucky >= 3 then
sleep(500)
doSendMagicEffect(getPlayerPosition(cid), 31)
doPlayerSendTextMessage(cid, 21, "Haha you lost")
end
doTransformItem(getTileItemById(frompos, 9902).uid, 9901)
end)
else
doPlayerSendCancel(cid, "You dont have a CCs...")
end
end
have fun :D
 
@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..
 
Last edited:

Similar threads

Back
Top