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?

sus, (edited )

found a few examples of how to do it on a github issue:


<span style="color:#323232;">var script = GDScript.new()
</span><span style="color:#323232;">script.source_code = "func say_hello():ntprint("Hello!")"
</span><span style="color:#323232;">script.reload()
</span><span style="color:#323232;">var script_instance = script.new()
</span><span style="color:#323232;">script_instance.call("say_hello")
</span>

<span style="color:#323232;">
</span><span style="color:#323232;">var MyClass = load("myclass.gd")
</span><span style="color:#323232;">var instance = MyClass.new()
</span><span style="color:#323232;">assert(instance.get_script() == MyClass)
</span><span style="color:#323232;">
</span>
Inductor,

Are you perhaps looking for an Expression?

I think that is probably as close as you can get to what you want to do.

Smorty, (edited )

This seems to be the exact thing I am searching for, thanks! I have run into an interesting hickup tho. One can pass self to the expression so that it uses the node as the base, but the expression doesn’t seem to have access to autoloads. The error I’m getting there looks something like this: Invalid named index ‘ExampleAutoload’ for base type Object EDIT: Turns out I need to pass these individually as arguments essentially.

tiny_electron,

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

Smorty,

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

pohart,

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,

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

  • All
  • Subscribed
  • Moderated
  • Favorites
  • godot@programming.dev
  • khanakhh
  • magazineikmin
  • osvaldo12
  • GTA5RPClips
  • mdbf
  • Youngstown
  • tacticalgear
  • slotface
  • rosin
  • kavyap
  • ethstaker
  • everett
  • thenastyranch
  • DreamBathrooms
  • megavids
  • InstantRegret
  • cubers
  • normalnudes
  • Leos
  • ngwrru68w68
  • cisconetworking
  • modclub
  • Durango
  • provamag3
  • anitta
  • tester
  • JUstTest
  • lostlight
  • All magazines