Adding an instance of a scene as a child multiple times?

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

Hello wonderful members of the community, im here to ask about the title basically.

Im making a top-down 2D action game, with a system of picking up and dropping weapons.

I could manage to make a pistol which starts on the floor, and the player can pick up and drop, this works great, but now i tried making the enemies drop their weapons when they die, using the same method for the player dropping.

This creates another “Pistol (pickup)” in the world BUT after that i always pick up “that” pistol, even if im standing on the other one.

The relevant code is the following:

var ppistol = preload("res://Pistol (pickup).tscn")

This happens when the player right clicks, while holding a pistol:

var drop = ppistol.instance() 
get_node("/root/World").add_child(drop) 
drop.set_position(get_node("/root/World/Player").get_position()) 

Then when the player weapon’s raycast hits an enemy:

$Pistol/RayCast2D.get_collider().get_node("Pistol").queue_free()
get_node("/root/World").add_child(ppistol.instance())

(I didnt write a “set_position” for this one yet)

Anyways… as you can see, both time i use “get_node(”/root/World").add_child(ppistol.instance())" to spawn a pistol, so im guessing this needs to be different.

The only solution i could come up with was add several vars named “ppistol1” “ppistol 2” and so on, but this is very bad code and im sure there are other, much better, ways that i havent learned yet.

Thanks in advance!