What do I need to do to make a health "component" in Godot?

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

I can’t find any tutorials about this, I’m looking for something that has the functionality of a “component” in other engines. I’d like to have something that I can quickly import/insert/load/put into any node and make it have health that can be changed externally. How would I go about that?

:bust_in_silhouette: Reply From: kidscancode

You can use a custom Resource for this. There is information about this in the docs:

I have also created a brief example to demonstrate:

:bust_in_silhouette: Reply From: jkf16m

Hi, what I’ve been doing at least in c#.

It’s exposing the component of the concrete node type that
will compose the Health component.

What I mean, Health component must have a clear interface of what its parent should be.

I like to distinct the parents as “Entities” and children as “Components”.
And also the complex ones, they are both entities and components at the same time.

But only components will define the behaviour of their parents.

And the parents should only be of one clear interface, concrete Node class.

This might be hard to grasp, I’ll try to explain it with text

–Health: It adds health to a Node parent
–Defense: It adds defense to a Node parent
–VulnerabilityArea2D: An area2D that defines the ranges of vulnerability
----Vulnerability: It adds vulnerability to a given list of groups, requests both health and defense nodepaths.
–Attack: It adds attack to a Node parent

//I’m working this implementation
–StateMachine: It adds a state machine to the parent

For the state machine, I haven’t been able to make it work, so I plan on making a mediator for that part.

For now in your projects, making a state machine direct child and the states direct children of the main entity would be much easier.

:bust_in_silhouette: Reply From: aidave

You may find that using Composition is a better approach:

It’s how Godot/GDScript is designed to work, although personally I think using it for something simple like health is overkill, you can do whatever you want!