0 votes

Hi all.

I'm trying to get a pair of guns to fire at the same time. Problem is, even though I have it mostly set up, I can't figure out how to keep it from fussing about an instance already being a child of 'root'.

player.gd

extends KinematicBody2D

const attack = preload("res://Game Scenes/playershot.tscn")
var playerAttack = attack.instance()
...
func shoot():
    var shot_pos_1 = $GunOne.global_position
    var shot_pos_2 = $GunTwo.global_position

# The actual shooting section \/ Needs a little help.
    playerAttack.start(shot_pos_1)
    get_parent().add_child(playerAttack)
    playerAttack.start(shot_pos_2)
    get_parent().add_child(playerAttack)

Whenever I try to get the projectiles to spawn, the Debugger says:
E 0:00:03.501 addchild: Can't add child 'PlayerShot' to 'root', already has a parent 'root'.
<C++ Error> Condition "p
child->data.parent" is true.
<C++ Source> scene/main/node.cpp:1176 @ add_child()
player.gd:45 @ shoot()
player.gd:16 @ _input()

in Engine by (197 points)

2 Answers

+1 vote
Best answer

You've only made one instance, because you call it when initializing the variable. The error is telling you you can't put the same instance in two places and add it to the tree twice. You need to call instance() every time you make a new object:

extends KinematicBody2D

const attack = preload("res://Game Scenes/playershot.tscn")
#### Get rid of this: var playerAttack = attack.instance()
...
func shoot():
    var shot_pos_1 = $GunOne.global_position
    var shot_pos_2 = $GunTwo.global_position

# The actual shooting section \/ Needs a little help.
    for shot_pos in [shot_pos_1, shot_pos_2]:
        var playerAttack = attack.instance()
        playerAttack.start(shot_pos)
        get_parent().add_child(playerAttack)
by (20,313 points)
selected by

Looks like I was too slow... I didn't intend to add an additional variation to an already correct answer, however I don't see a way to remove it now.

Multiple answers are fine (especially if they're correct). :)

Sure, though I wouldn't have added my response if I had seen yours. I think I must have responded in a "stale" tab where your reply wasn't yet visible. Thanks.

Hey, thanks to both of you.

+1 vote

It looks like you've only created a single instance of the attack scene, and are then trying to add it to the tree twice. You can't do that.

I assume you probably want to create 2 separate instances of the attack scene, and add the 2 instances to the tree. So, something like:

var playerAttack1 = attack.instance()
var playerAttack2 = attack.instance()
.
.
get_parent().add_child(playerAttack1)
get_parent().add_child(playerAttack2)
by (10,882 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.