Awaiting Godot function called form Lua #80
-
|
I am working on a game where you can code a robot to perform automation tasks. For example, I have this code to run the lua.globals["move"] = await func(dir: String):
if dir != "forward" and dir != "backward":
print_console("Error: move() direction must be 'forward' or 'backward'\n")
return
await call_deferred("move", dir)
func move(dir: String):
print_console("moving %s\n" % dir)
var direction := robot.transform.basis.z.normalized()
robot.position += (-direction if dir == "forward" else direction) * 2
await get_tree().create_timer(0.5).timeout # 0.5 second cooldownIf I run this script, the player will move 10 times instantly, instead of waiting for the for i = 1,10 do
move("forward")
endThe full script:extends CodeEdit
@export var robot: Node3D
@export var console: Label
func _on_button_pressed() -> void:
console.text = "" # reset console
var lua = LuaState.new()
lua.open_libraries(LuaState.LUA_BASE)
lua.open_libraries(LuaState.LUA_MATH)
lua.open_libraries(LuaState.GODOT_VARIANT)
lua.globals["move"] = await func(dir: String):
if dir != "forward" and dir != "backward":
print_console("Error: move() direction must be 'forward' or 'backward'")
return
await call_deferred("move", dir)
lua.globals["turn"] = await func(dir: String):
if dir != "left" and dir != "right":
print_console("Error: turn() direction must be 'left' or 'right'")
return
await call_deferred("turn", dir)
lua.globals["print"] = func(text):
print_console(str(text))
var result = lua.do_string(text)
if result is LuaError:
print_console("Lua Error: " + str(result))
func move(dir: String):
print_console("moving %s" % dir)
var direction := robot.transform.basis.z.normalized()
robot.position += (-direction if dir == "forward" else direction) * 2
await get_tree().create_timer(0.5).timeout
func turn(dir: String):
print_console("turning %s" % dir)
robot.rotate_y(PI / 2 if dir == "left" else -PI / 2)
await get_tree().create_timer(0.5).timeout
func print_console(txt):
console.text += txt + "\n"
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
|
Hey @TennoAntenno, thanks for the question. Unfortunately, right now there's no mechanism in Lua that mimics GDScript's |
Beta Was this translation helpful? Give feedback.
-
|
Hey @TennoAntenno, so I've been trying to implement the The First of all, you'd need to run the script in a coroutine instead of using |
Beta Was this translation helpful? Give feedback.
Hey @TennoAntenno, thanks for the question.
Unfortunately, right now there's no mechanism in Lua that mimics GDScript's
await(it's in the plans, not sure when this will happen though).I think theoretically you could use a Coroutine and yield when calling the GDScript methods, then resuming the coroutine when the timers end, but this is certainly not ideal and I'm not sure there are sufficient infrastructure for doing this in GDScript (although one could always open the LUA_COROUTINE library and call
coroutine.yield()for yielding).