Hi everyone.
First post here, let me know if this is consistent with the forum rules and guidelines.
I've been refactoring a bit the code to include more "composite" approach, as demonstrated on a youtube video from Bitlytic
I like the "Lego" approach where I can simply attach behavior to entities by drag and droppin' prebuild components (scenes) to them, rather than relying solely on inheritance.
For my Example, I have a Character Class. and this node has a child scene included "HealthComponent".
HealthComponent as a built in script & class which manage Health of the component. It's logic is supposedly limited to managing Health, with methodes like damage(hp: int), heal(hp: int); etc.
Not every Character has a Health Component. Health component is supposed to be optional.
my problem is whenever a Character could take damages. I want to be able to call health.damage(3) directly from the Character.
I'm unsure of the best approach here to reference the component on the parent level.
@onready var health: HealthComponent =$HealthComponent
won't work because Character may not have a HealthComponent.
should I use, within Character, something like
@onready var health: HealthComponent = get_node_or_null("HealthComponent") as HealthComponent
or
@export var health = null
and assign this within the UI for each Character (or more likely anything that derivates from Character ?)
or something else ?
is there a scenario where I could call health.damage() from my Character without having to test if health is empty ?
I understand there are possibly multiple approaches with various pros and cons. Curious about your opinion and possibly best practices.
Thanks for your help !