0 votes

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
---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

in Engine by (394 points)

2 Answers

+3 votes
Best answer

get_child gets a child by index.

by (3,219 points)
selected by
+3 votes

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

by (805 points)

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.

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.

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.