Easy way to get certain type of children ?

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

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

:bust_in_silhouette: Reply From: LoneDespair

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

EnemyInstance.add_to_group(“SpecialEnemy”)
You could assign them through inspector for less code

EnemyReference.is_in_group(“SpecialEnemy”)

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.

GameVisitor | 2019-11-11 12:42

:bust_in_silhouette: Reply From: luislodosm
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")