Jump to content

[SOLVED] How to call Sleep function from `modmain`


Recommended Posts

I've seen some mods use the `Sleep` function, but when I try to use it in the modmain.lua file I get an error:

modmain.lua:
 

function main()
    AddPlayerPostInit(
        function()
            GLOBAL.TheInput:AddKeyDownHandler(
                string.byte("z", 1),
                function()
                    print("BEFORE")
                    Sleep(10)
                    print("AFTER")
                end
            )
        end
    )
end
main()

Error:

[00:16:44]: [string "../mods/lib/modmain.lua"]:9: attempt to call global 'Sleep' (a nil value)
LUA ERROR stack traceback:
    ../mods/lib/modmain.lua:9 in (field) fn (Lua) <7-11>
    scripts/events.lua:46 in (method) HandleEvent (Lua) <42-49>
    scripts/input.lua:187 in (method) OnRawKey (Lua) <184-191>
    scripts/input.lua:396 in () ? (Lua) <395-397>

 

EDIT: using GLOBAL.Sleep:

[00:02:53]: attempt to yield across metamethod/C-call boundary
LUA ERROR stack traceback:
    =[C]:-1 in (field) yield (C) <-1--1>
    scripts/scheduler.lua:339 in (field) Sleep (Lua) <336-343>
    ../mods/lib/modmain.lua:6 in (field) fn (Lua) <4-8>
    scripts/events.lua:46 in (method) HandleEvent (Lua) <42-49>
    scripts/input.lua:187 in (method) OnRawKey (Lua) <184-191>
    scripts/input.lua:396 in () ? (Lua) <395-397>
    

 

Edited by danielpa
Link to comment
Share on other sites

:D
Just yesterday I solved the mystery of Sleep().
Take a look at game scripts that use it (not many), but for example within the talker component.

You have to start the function that uses Sleep with a thread.
Sleep does pauses the current function and waits x seconds. If you would call this without a thread, you would pause the whole game, cause everything is called in row. So to be able to call Sleep you have to start a parallel task that is independed of the game functions and there you can Sleep now.

Edited by Serpens
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...