How to get child from parent if the child is instanced.

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

I have 2 gun scenes and the player adds them as a child on a position2d node. I have a hud that show the payers hp and ammo but currently im trying to figure out how to get the gun scene child because it could be any gun so i dont have a direct path to it.

My scene tree is like this
World
-player
–position2d
—chosen gun(pistol, minigun)

The gun will be instance into the scene depending on what was equiped.

Are the any methods that can get me the gun child even without knowing the node name

:bust_in_silhouette: Reply From: Magso

get_child gets a child by index.

position2d.get_child(0)
:bust_in_silhouette: Reply From: Sween123

Create an array for the “chosen gun” scene
When you instance a gun, add the intanced scene to the array.

This would set up a way to pick up, drop and switch guns. So keeping the selected index would also get the gun currently being used.

Magso | 2020-02-09 10:16

Yes, Can also use an array of indexes.
Also, can use an array of dictionaries: [{“node”, “info”…}, …] to keep guns and all infos in “chosen gun” scene.

Sween123 | 2020-02-09 10:40