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:

var new_node:Node = Node.new()
add_child(new_node)
var new_script:GDScript = GDScript.new()
new_script.source_code = "extends Node\nfunc run():\n\t" + script_to_execute
print(new_script.source_code)
new_node.set_script(new_script)
pressed.connect(new_node.call.bind("run"))

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?

  • @tiny_electron
    link
    182 months ago

    This sounds like a dangerous idea (arbitrary code execution by an attacker), what is your use case?

    • Smorty [she/her]OP
      link
      fedilink
      102 months ago

      This is really just an internal tool I want to build. This will not be exposed to the player,

      • @[email protected]
        link
        fedilink
        52 months ago

        If it’s connected to a network it’s not enough that it’s an internal tool. Don’t open yourself up to a coworker executing code on your machine as you.

        • Smorty [she/her]OP
          link
          fedilink
          72 months ago

          Okay this is reasonable… I should have clarified that i am the only worker (don’t own a company or anything) so i think it’ll be fine