# Programmatically created instance does not execute any code

I've created a new instance, positioned it, and added it to the scene like this:

const Player = preload("res://player.tscn")

func _physics_process(delta):
if <outside display>:
var newplayer = Player.instance()
newplayer.position.x = 200
queue_free()


This works to the extent that the player is created in the right position and starts falling down with gravity. But the new player does not respond to any input, nor does it do anything if I print things in the _ready function or the _physics_process function. It seems its script is just never executed at all.

I've tried not queue_free()'ing the old one, in case removing it is like removing its parent, but that doesn't help.

I've tried calling the ._ready() function on it manually, but that doesn't work either.

I've tried calling request_ready() on the new player, but that doesn't run the _ready() function either.

In tutorials, this is usually all they do to get a new instance into the world, so I'm not sure what I'm missing here.

• Is that code part of your player's script or of another node's script? Please post all relevant code and the node structure. The code you've posted should actually work, so something important seems to be missing. – skrx Jul 7 '18 at 15:14
• @skrx That's the player's script. The only other bit of relevant code would be the set_physics_process(true) in the _ready function. It's the only script I have at the moment. My root node is world (just a node) with player (RigidBody2D) as only relevant child (and some unused, static object called target as other child). The player has a Sprite and CollisionShape2D as children. Any idea what could be missing? – Luc Jul 7 '18 at 22:28
• You wrote, "the new player does not respond to any input", but there's no input handling in your code. What do you expect to happen? Also, what's the actual code for <outside display>? -- I've created a simple example similar to yours in which I just move the player object to the right and spawn new players by pressing a key and it works correctly. BTW, the preload causes an error possible cyclic resource inclusion, but the program still works so I'm not sure if it has to do with the problem (I replaced it with load). – skrx Jul 8 '18 at 18:06
• @skrx Oh right, I forgot that that's not in the code I posted. This is the full code for the player (the only script in the game so far): hastebin.com/oxomiwofux.vbs There's probably a lot to be improved but I'm having trouble finding documentation (e.g. I feel like the manual sin and cos calculation is probably unnecessary). Anyway, input handling is done there, and the whole thing is not executed for newly created instances if I don't call newplayer.set_script(...). – Luc Jul 8 '18 at 22:30
• Something is missing again in your new example: The state variable and the STATE enum. I've commented out the lines if state != STATE.DEAD: and newplayer.set_script(get_script()) and it works correctly for me, so I guess the problem could have to do with the state variable. Where do you set or reset it? – skrx Jul 9 '18 at 12:22

After browsing the autocomplete options some more, I found set_script(). It is apparently to be used like this:

newplayer.set_script(script_object)


I'm not sure how to find an arbitrary script object, but in case you want to attach the same script as is currently running, you can do this:

newplayer.set_script(get_script())


This needs to be done before adding it to the scene.

The full code is:

const Player = preload("res://player.tscn")

func _physics_process(delta):
if <outside display>:
var newplayer = Player.instance()
newplayer.position.x = 200
newplayer.set_script(get_script())