Simple sleep #60
Replies: 4 comments 4 replies
-
|
You can implement a sleep in the environment pretty easily by yielding the
coroutine and passing a float/int as an argument, and parsing that in
GDscript.
…On Fri, Mar 28, 2025, 11:56 AM OvercookedBeef ***@***.***> wrote:
Is there any way to make a simple sleep function?
eg.
print("A")sleep(2)print("B")
I actually started using my own language (which transpiles to Lua), since
I think Lua may be too complex/annoying for some people, especially if they
have do to the format below over and over:
print("A")run_after(2, function()
print("B")end)
—
Reply to this email directly, view it on GitHub
<#60>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABMKIGL5BVQ3G2CTQABQTQT2WVWJBAVCNFSM6AAAAABZ7367I2VHI2DSMVQWIX3LMV43ERDJONRXK43TNFXW4OZYGE2DCMRVG4>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
|
So, I can share some explicit sample code when I'm not typing from mobile,
but if you built a function in Lua, `function sleep(t) ... end`
and have it call coroutine.yield("SLEEP",t), you can manage the coroutine
from GDscript, by getting the return from LuaCoroutine.resume(...).
…On Fri, Mar 28, 2025, 12:19 PM OvercookedBeef ***@***.***> wrote:
You can implement a sleep in the environment pretty easily by yielding the
coroutine and passing a float/int as an argument, and parsing that in
GDscript.
Thanks, I'm trying to understand this as I don't have any experience with
coroutine in Lua. Are you saying I should have a sleep global, of the
type LuaCoroutine, which I pass in from GDScript, and they then can do
sleep.yield(1.5)? I'm looking at the Lua docs for (coroutine)[
https://www.lua.org/pil/9.1.html] and it looks like yield is typically
done from *inside* the coroutine, so I don't think I'm understanding
correctly
—
Reply to this email directly, view it on GitHub
<#60 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABMKIGITTJLTICBHVBRM6BT2WVZBRAVCNFSM6AAAAABZ7367I2VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTENRVGUZDENQ>
.
You are receiving this because you commented.Message ID:
***@***.***
com>
|
Beta Was this translation helpful? Give feedback.
-
|
You can sleep using a timer created in Godot and coroutines, although being able to re-call the calling function from Godot is a bit hacky. Here's two examples, one as a Lua Node and the other from GDScript. -- As a Lua Node
local node = {
extends = Node
}
function node:_resume(co) -- Helper function to resume a coroutine
coroutine.resume(co)
end
local function wait(self, time)
local co = coroutine.running()
if co then
-- Calls this Node with the corresponding coroutine
self:get_tree():create_timer(time).timeout:connect(Callable(self, "_resume"):bind(co))
coroutine.yield() -- Yield the coroutine. The timer above will resume it
else
error("Tried to wait on non-coroutine.")
end
end
node._ready = coroutine.wrap(function(self) -- Note this must be wrapped
print("Hello from Lua Node at " .. Time:get_ticks_msec() / 1000.0)
wait(self, 1)
print("Hello again from Lua Node at " .. Time:get_ticks_msec() / 1000.0)
end)
return node
# In GDScript
var lua_state := LuaState.new()
lua_state.open_libraries(
LuaState.Library.LUA_BASE |
LuaState.Library.GODOT_UTILITY_FUNCTIONS | # For printing
LuaState.Library.GODOT_ALL_LIBS | # For Time library
LuaState.Library.LUA_COROUTINE) # Coroutines, necessary
var yieldable = lua_state.do_string("""
local function wait(time)
coroutine.yield("sleep", time)
-- Passing "sleep" as a flag just so we can differentiate other
-- yields if needed
end
-- Note I'm creating a coroutine here instead of wrapping.
-- This is mostly personal preference
return coroutine.create(function()
print("Hello from Lua at " .. Time:get_ticks_msec() / 1000.0)
wait(1) -- We don't need to pass self here because yielding is handled in GDScript
print("Hello again from Lua at " .. Time:get_ticks_msec() / 1000.0)
end)
""")
var callback := func(cb):
var result = yieldable.resume()
if result is Array and result[0] == "sleep":
get_tree().create_timer(result[1]).timeout.connect(cb)
callback.call(callback.bind(callback)) # I don't know the elegant GDScript solution to thisYou can probably put together a nicer interface, but the problem is really that the Lua->Godot interface can be a little clunky because of how differently the languages act. Note that from the changes #80, it might be easier to handle with with awaiting, but I haven't tested that yet. |
Beta Was this translation helpful? Give feedback.
-
|
Hey folks! Sorry about the late response. Yeah, using the new and unreleased So as already noticed by @Denneisk, I explain in #80 (comment) that I have been experimenting with the implementation of Here's a quick test, seems to be working well for timers: Note: |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Is there any way to make a simple sleep function?
eg.
I actually started using my own (very simple, but enough for my usecase) language (which transpiles to Lua), since I think Lua may be too complex/annoying for some people, especially if they have do to the format below over and over: (among some other minor things)
Beta Was this translation helpful? Give feedback.
All reactions