+5 votes

Is there an easy way to get a certain type of children of a node ?

Say I have a spatial node with children of different structure, ex:

spatial node: (3 children)
- Enemy 1 => 2 rigid bodies, 3 meshes
- Enemy 2 => 1 rigid body, 1 mesh
- Enemy 3=> 3 rigid bodies, 5 meshes

I want to "select" a certain type of Enemy children and do a modification,
ex: set mesh textures to null, or change rigid body to static...

Is there an easy way to do that, without looping over each enemy manually and so on?

Thx in advance

in Engine by (892 points)

2 Answers

+2 votes

You could assign them to a group e.g("Enemy") and check if they belong in there

EnemyInstance.addtogroup("SpecialEnemy")
You could assign them through inspector for less code

EnemyReference.isingroup("SpecialEnemy")

by (410 points)

Thx for ur reply, but i am not looking to select / manipulate the enemies per se.

In one case I want to change the (6) rigid bodies to static
In another case, i want to change the (9) meshes' color to red.

+7 votes
var desired_children = []
for child in get_children():
    if child is DesiredClass:
        desired_children.append(child)

or

 desired_children = get_tree().get_nodes_in_group("desired_group")
by (320 points)
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 Frequently asked questions and 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 [email protected] with your username.