Running a string as a script?
Is there any way to run a given string at runtime? I have tried to write something like this by creating a new node and attaching a new script to it, and I have gotten this far:
<span style="color:#323232;">var new_node:Node = Node.new()
</span><span style="color:#323232;">add_child(new_node)
</span><span style="color:#323232;">var new_script:GDScript = GDScript.new()
</span><span style="color:#323232;">new_script.source_code = "extends Nodenfunc run():nt" + script_to_execute
</span><span style="color:#323232;">print(new_script.source_code)
</span><span style="color:#323232;">new_node.set_script(new_script)
</span><span style="color:#323232;">pressed.connect(new_node.call.bind("run"))
</span>
This gets an error (but not a crash) when hitting that last line:
emit_signalp: Error calling from signal ‘pressed’ to callable: ‘Node::call’: Method not found.
This is pretty weird, as a Node should always have access to the method “call”. Does someone have an idea on how to solve this / their own idea on how to implement runtime code editing and executing?
Add comment