Replies: 1 comment
-
Hi @JekSun97 !
Well, not out of the box, Lua functions are only directly callable from Lua scripts. But it's easy enough to make a local LuaRunner = {
extends = 'Node'
}
function LuaRunner:do_string(code)
-- using `tostring` here is necessary to convert Godot String -> Lua string
local func = assert(load(tostring(code)))
return func()
end
function LuaRunner:do_file(code)
-- using `tostring` here is necessary to convert Godot String -> Lua string
local func = assert(loadfile(tostring(code)))
return func()
end
return LuaRunner If you register this extends Node
func _ready():
LuaRunner.do_string("print 'Hello from Lua!'") I haven't tested this code, but I think it will work fine =] If can also sandbox the environment of the loaded code, to avoid user code crashing your game, accessing the file system, this stuff that malicious code might attempt to do. If it is just for an experiment/for fun, it's ok to overlook this, but you should most definitely worry about sandboxing if shipping this product to end users. |
Beta Was this translation helpful? Give feedback.
-
is it possible to read lua code from gdscript? I'm making a programmer's simulator where you need to write code in a game in lua
Beta Was this translation helpful? Give feedback.
All reactions