Duplicate a node with sub-nodes and change position

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

I’m making a game where coins spawn, one of the characters of the game walks around the scene and spawn coins, due to some functions, I decided to create like a core coin that is cloned and then changes the position of the cloned coin to the character’s position.

I tried using duplicate() but didn’t work, I thought it doesn’t duplicate sub-nodes so I decided testing cloning the sprite and changing its position, the console says the position where it was changed, but in-game it doesn’t show in that position.

Basically, the code is like this:
func on_timeout(): var thingy = get_parent().get_node("Group/Coin") var thingyTwo = thingy.duplicate() thingyTwo.position.x = 0 print(thingyTwo.position.x)

:bust_in_silhouette: Reply From: razah

you duplicated it but did not add it to the scene tree.

func on_timeout():
    	var thingy = get_parent().get_node("Group/Coin")
    	var thingyTwo = thingy.duplicate()
     	thingyTwo.position.x = 0
    	print(thingyTwo.position.x)

for example;

func on_timeout():
    	var thingy = get_parent().get_node("Group/Coin")
    	var thingyTwo = thingy.duplicate()
     	thingyTwo.position.x = 0
     	someNode.addchild(thingyTwo) #Add to scene tree
    	print(thingyTwo.position.x)
1 Like