0 votes

Hello.

How to do this?

weapon_resource = preload("res://scene/weapon/Sword.tscn")
weapon = weapon_resource.instance()
get_node("AttackDown/PathFollow2D").add_child(weapon)
get_node("AttackUp/PathFollow2D").add_child(weapon)
get_node("AttackLeft/PathFollow2D").add_child(weapon)
get_node("AttackRight/PathFollow2D").add_child(weapon)

This code gives me these errors:

Can't add child 'Sword' to 'PathFollow2D', already has a parent 'PathFollow2D'.

Type:Error
Description: Can't add child 'Sword' to 'PathFollow2D', already has a parent 'PathFollow2D'.
Time: 0:00:02:0202
C Error: Condition ' pchild->data.parent ' is true.
C Source: scene\main\node.cpp:1331
C Function: Node::add
child

It seems like only one child is added, but why is that? The paths are different, so why can't I add the same scene to a different path?

asked Feb 19, 2018 in Engine by Sumerechny (32 points)

1 Answer

+2 votes
Best answer

A Node can be a child of only one parent, it's a tree structure:
https://en.wikipedia.org/wiki/Tree_(data_structure)

You need to have multiple instances of "res://scene/weapon/Sword.tscn", one of each add_child() you want to call.

Put this line:

weapon = weapon_resource.instance()

before every call of add_child().

answered Feb 19, 2018 by hilfazer (2,154 points)
selected Feb 19, 2018 by Sumerechny

Thanks! Another thing to keep in mind.

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.