In Godot, there’s something I find puzzling: the engine encourages behavior composition through various functional components (Nodes), but it doesn’t seem to provide an efficient way to filter or query for these components.
For example, a common implementation might look like this:
func _on_body_entered(body):
if body.has_method(“take_damage”):
body.take_damage()
However, in a composition-based design, take_damage would ideally reside in a separate CanDamageComponent node, rather than being directly in the root body.
This creates a need to first determine whether the body contains a CanDamageComponent node. The typical approach would be something like:
var damage_component = body.find_child(“CanDamageComponent”)
But using node names like “CanDamageComponent” for searching is unreliable, as the name might differ.
A more logical approach would be to define the function signature like this:
func _on_body_entered(body: (with CanDamageComponent))
which would inherently filter entities based on whether they contain the desired component.maybe by component name ,or interface name and so on.
The issue becomes even more complex in situations of using area like:
func _on_area_entered(area: Area3D):
Locate CanDamageComponent in the associated entity
Here, it’s necessary to first locate the entity through the area, and then find the damage component within the entity, making the process even less efficient.
Of course, it’s possible to record mutual references between the entity and its components, but this approach feels inelegant and wasteful.
If I’m mistaken or there are better approaches, I’d appreciate any corrections or suggestions. Thank you!