TL;DR:
Now I check the existence of certain child by node name: has_node("HealthComponent") where HealthComponent is a name of a node
Is there any way to check this by child's class? Something like: includes(HealthComponent) where HealthComponent is a class
In my project I am trying to stay in the philosophy of Entity Component System. It seems to go well with Godot Nodes.
It means that every 'kind of functionality' is its own node, that can be attached to any number of things and still provide the same functionality.
So. To check if this entity has this kind of functionality I must check, whether it has a certain child Node of the required class. And to do that, I currently use has_node(). Like this:
#attaching a signal to update unit's target in attack component
if unit_instance.has_node("AttackComponent"):
game_scene.select_attack_target.connect(unit_instance.attack_component.update_attack_target)
but it feels... clunky. Is there a better way, maybe?
How do I check if the target 'entity' has a child node of a certain class?
Here's how I built my system:
Every Component declared as its own class.
Every entity that has components, has a variable for each one.
But that's pretty lot of manual work when creating or changing any kind of game entity, or accessing their functionalities from other components.
extends CharacterBody2D
class_name Unit
@export var selection_component : SelectionComponent
@export var movement_component : MovementComponent
@export var abilityProgress : ProgressBar
@export var nav_agent : NavigationAgent2D
@export var health_component : HealthComponent
@export var attack_component : AttackComponent

My own idea (does not feel very good to me):
- A kind of global list (Enum?) of all existing component classes (but how to pass them as class names?)
- In each entity, an array of the enum length. Array[n], where n is a value taken from enum by class name
- Array filled on _ready() via get_children() and checking them against list of classes in Enum
- A function that returns a child from the array (if the class fits) or null
Does this look any realistic?..