A few more questions #126
-
(Continuation of this discussion) Hi again!
Thanks again for your time and for building this extension. It’s been incredibly helpful! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Hey @TennoAntenno , welcome back!
You can use a count hook to yield coroutines whenever they execute You can setup count hooks in GDScript like so: const COUNT = 1000
func _coroutine_hook_func(debug_info: LuaDebug):
prints("The coroutine ran", COUNT, "instructions, yielding...")
# Returning HOOK_YIELD here makes the coroutine yield
return LuaThread.HOOK_YIELD
var my_coroutine: LuaCoroutine = ...
my_coroutine.set_hook(_coroutine_hook_func, LuaThread.HOOK_MASK_COUNT, COUNT)
my_coroutine.resume()
# After the first resume, if the coroutine isn't finished, it will yield automatically after COUNT instructions.
# You can then re-resume it or cancel / just discard it as necessary
The environment table is the "global" table used by the loaded script. If you get/set "global" values from Lua code, they will actually access the environment table, which by default is Here's an example GDScript that passes an env table that contains only var lua = LuaState.new()
lua.open_libraries()
var script_env: LuaTable = lua.do_string("""
-- script env contains only the "print" global function, so scripts will only be able to access it
return { print = print }
""")
var script = lua.do_string("""
-- by passing "script_env" as this script's env, it is used instead of _G for global access (both set/get)
print(_G == nil) -- true, since "_G" is not present/accessible in script_env
print(print ~= nil) -- true, "print" is the only thing in the env
some_global = 5 -- sets script_env["some_global"] to 5
""", "", script_env)
print(script_env.some_global) # 5 Now you can cherry pick the exact functions you want to the script by only providing them in the environment. Even if the global Lua table contains all utility functions, the script won't be able to access them.
Hmm, that may be a bug, I'll check it out later.
Well, if you prepended some lines to the script, then the line numbers are technically correct.
No problem, thank you for using it! |
Beta Was this translation helpful? Give feedback.
Hey @TennoAntenno , welcome back!
You can use a count hook to yield coroutines whenever they execute
count
instructions (added in Lua GDExtension 0.4.0). This fixes the infinite loops, as well as make it possible for you to just discard the coroutine without ever resuming it, for example with the press of a button.You can setup count hooks in GDScript like so: