-1
\$\begingroup\$

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
        get_tree().get_root().add_child(newplayer)
        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.

\$\endgroup\$
9
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 7, 2018 at 15:14
  • \$\begingroup\$ @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? \$\endgroup\$ Commented Jul 7, 2018 at 22:28
  • 1
    \$\begingroup\$ 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). \$\endgroup\$ Commented Jul 8, 2018 at 18:06
  • \$\begingroup\$ @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(...). \$\endgroup\$ Commented Jul 8, 2018 at 22:30
  • 1
    \$\begingroup\$ BTW, please don't post links to your files. Just add the relevant information (.gd and .tscn files) to your post. \$\endgroup\$ Commented Jul 10, 2018 at 19:45

1 Answer 1

1
\$\begingroup\$

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())
        get_tree().get_root().add_child(newplayer)
        queue_free()
\$\endgroup\$
2
  • \$\begingroup\$ You can accept your own answer, if this suffices. \$\endgroup\$ Commented Dec 13, 2019 at 3:27
  • \$\begingroup\$ I don't think this is a good example for others to follow. There's some missing code here that's causing the problem. (You shouldn't have to re-assign scripts.) \$\endgroup\$ Commented Oct 3, 2023 at 17:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.