Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to execute text from textEdit's text property with loadstring() #49

Open
Oolks opened this issue Mar 25, 2023 · 1 comment
Open

Comments

@Oolks
Copy link

Oolks commented Mar 25, 2023

local Executor = {
	extends = "Node",
}

function Executor:execute(code)
	if code == nil or code == "" then
		return false
	end
	-- Code to execute
	local temp_code = loadstring(code)
	
	-- Execute the code and capture the results
	local success, result = pcall(temp_code)
	
	-- Check if there was an error and print it if necessary
	if not success then
		print("Error executing code:", result)
		return false
	end
	
	-- Print the output of the code if there is any
	if result ~= nil then
		print("Output:", result)
	end
	
	return true
end

function Executor:_process()
    local value = self:get_parent():get_parent().play
	if value == true then
		local code_to_run = self:get_parent():get_parent():get_node("CoreUI/ScriptEditor/TextEdit").text
		print(type(code_to_run))
		
		self:execute(code_to_run)
	end
end

return Executor```
it keeps on giving me error of:

E 0:00:13.272 loadstring: bad argument #1 to 'loadstring' (function expected, got cdata)
stack traceback:
[string "res://Scripts/Core/Scripting/Executor.lua"]:10: in function 'execute'
[string "res://Scripts/Core/Scripting/Executor.lua"]:35: in function <[string "res://Scripts/Core/Scripting/Executor.lua"]:29>
in call _process @ "res://Scripts/Core/Scripting/Executor.lua"

Executor.lua:10 @ loadstring()
@gilzoide
Copy link
Owner

Hi @OoIks.
This happens because Godot Strings are not translated to Lua strings automatically (cdata is the name LuaJIT gives to any FFI-defined type, which includes anything related to Godot).
I let it be like that because it avoided marshalling (copying/converting data to/from Lua/Godot) and Godot's String class has more functionality than Lua's built-in string type. In hindsight, this was probably a bad design choice, people would expect a string in Lua to be of Lua's string type, regardless if it comes from Godot data.

Now, to fix your problem right now, just call tostring passing your code variable and it will be converted to a Lua string, which loadstring accepts:

function Executor:execute(code)
        if code == nil or code == "" then
		return false
	end
	-- Code to execute
	local temp_code = loadstring(tostring(code))
                                  -- ^ here!

        -- ... rest of your code ...
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants