Why is the script not executed automatically?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By saulius

Why is the script not executed automatically and the function called? How do I force a script to run from a string?

extends MeshInstance 
var code="extends MeshInstance\nfunc _ready():\n\tpass\nfunc _process(delta):\n\tif Input.is_key_pressed(32):\n\t\tget_tree().get_root().find_node(\"kubas\", true, false).translate(Vector3(0.1,0,0))"
func _ready():
pass
func _process(delta):
    if Input.is_key_pressed(32):
        var script = GDScript.new()
        script.set_source_code(code)
        script.reload()
        var obj = MeshInstance.new()
        obj.name="qo"
        get_tree().get_root().add_child(obj)
        obj.set_script(script)

Why are you trying to do it like this?

Eric Ellingson | 2019-07-21 17:57