Getting child component

Godot Version

v4.2.2.stable.official [15073afe3]

Question

New to godot and trying to figure out components/signals,
I have many enemies that are all copies of the same scene, what’s the right way to only damage the correct one? I have a reference to the target enemy object (from a dynamic raycast result.collider.owner) that may or may not have a ‘health’ component child, depending if it’s an enemy or a wall. I’m trying to figure out how to only signal the one enemy/wall hit, instead of all enemies.

A lot of what I’ve found is saying to use get_node and call my takeDamage function directly, but it looks like that’s outdated/bad practise to use hardcoded strings?

Is there a ‘correct’ way to check if a node has a specific child component? I’m unsure if a signal vs calling directly is the right way to approach this.

You can group nodes.

Let’s say an enemy scene. Clicking on the root node of that scene, you can click on the Node tab.

image

Add a group name like “mobs” and in your code you can refer to this groups like this:

Example:

func _on_body_entered(body):
    if body.is_in_group("mobs"):
       body.get_hit(damage)
    else:
        # ignore

This worked, thanks. The other piece of the puzzle I was missing was storing a reference to the component in the root node’s script and using that.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.