How to add one specific scene as a child to multiple nodes at one time?

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

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 ’ p_child->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?

:bust_in_silhouette: Reply From: hilfazer

A Node can be a child of only one parent, it’s a tree 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().

Thanks! Another thing to keep in mind.

Sumerechny | 2018-02-19 16:32