v2.0-beta4
Pre-release
Pre-release
Whats changed
- Improved error handling
- New method
(LuaAPI/LuaCoroutine).set_hook
this allows you to set a Callable as a lua hook. See example below. - New method
LuaAPI.get_running_thread
. As the name implies this method returns the coroutine which is currently running. Intended to be called from a hook. - New method
LuaCoroutine.yield_state
As the name implies this method yields the coroutine state. Intended to be called from a hook. - LuaJIT support for gdExtension (only Linux, Windows and MacOS for now)
Hook code example:
extends Node2D
var lua: LuaAPI
var coroutine: LuaCoroutine
func _lua_hook(lua: LuaAPI, event: int, line: int):
var co: LuaCoroutine = lua.get_running_coroutine()
co.yield_state([1])
func _ready():
lua = LuaAPI.new()
# Despite the name, this is not like a OS coroutine. It is a coroutine.
coroutine = lua.new_coroutine()
coroutine.set_hook(_lua_hook, LuaAPI.HOOK_MASK_COUNT, 4)
coroutine.load_string("
while true do
print('Hello world!')
end
")
var yieldTime = 0
var timeSince = 0
var goodBye = false
func _process(delta):
timeSince += delta
# If the coroutine has finished executing or if not enough time has passed, do not resume the coroutine.
if coroutine.is_done() || timeSince <= yieldTime:
if !goodBye:
lua.do_string("""
for i = 0,10,1 do
print('Good Bye World!')
end
""")
goodBye=true
return
goodBye=false
# coroutine.resume will either return a LuaError or an Array.
var ret = coroutine.resume()
if ret is LuaError:
print("ERROR %d: " % ret.type + ret.message)
return
# Assumes the user will always pass the number of seconds to pause the coroutine for.
yieldTime = ret[0]
timeSince = 0