Health as a Component or as a Variable on a Character BP?

Hello,

In your opinions, is it best to have:

  • Health
  • Max Health
  • Is Dead? (boolean - health below 0 check)

As variables on a character? Or as components on a character?

Thanks

I’m not sure why you would make them components. I use variables for those…variables in my game

Lol that makes sense. Sorry if it was quite a silly question!

Well, I am quite new to this and had watched this Unreal tutorial:

Part of it near the end said:
“With this new component driven workflow, you do not have to just create a game, you can make a framework that makes building your game easier as you move forward. Now I could have people on my team whose job it was to come up with new things to live in my world, e.g. asteroids/space characters/boss characters. They could just simply drag and drop on the components that they need, or we can write new ones if we decide we need more behaviour.”

All of my Unreal projects so far have been just following tutorials from start to finish. I am hoping to practice what I have learnt (and learn more!) by rapid prototyping lots of small game ideas.

I am currently working on one project where there are many different classes of players and enemy AI. I thought that if I had created all of the behaviors and functionality as components, it would make it easier as I could just, say, drag and drop the health component onto each of them.

I have already created health and max health variables in my BaseCharacterBP, but I was wondering whether it would be easier to just have it as a component that I can drag and drop onto all of the other characters I have.

Then, when I start prototyping the next project, I can just pull of the components in and very quickly drop them onto anything necessary.

Kind of like creating a library of behaviors for myself. But I suppose I was probably going a bit overboard with considering it for such simple elements like health.

I have also just found this tutorial: Unreal Engine 4 Tutorial - Creating a Player Health Class and Library - YouTube but I haven’t watched it yet.

1 Like

Fantastic :slight_smile: thank you!

I know there are a couple ways to do what you’re trying to do. There are tags that you can look into using. You could, for example, tag an actor as an Enemy, and make all actors tagged as enemies follow certain rules (like taking damage, having health, etc.)
More relevant maybe using parent and child classes. For example, you can make a masterEnemyClass which comes with health, maxhealth, and dead variables already created. From that parent class you can create a child class that would inherit those same variables, but also allow for you to add more. For example, you could make a box class that has a cube for a static mesh. Then you could right-click that class, create child class, and make a red box class. The child inherits all the parents values, but can have its own additional values such as color on the static mesh that will not be applied to the parent

For something as generic as health, I’d put it in a component. The component could create damage numbers, handle elemental weaknesses, feed into a combat log, give points for kills, or perform any number of other useful health-related actions that you really shouldn’t rewrite constantly.

Sure, you could put that functionality on a base class, but inheritance-based structures start making functionality flow upwards as your game expands until tons of actors that don’t need the health code are running it and giving you a headache/slowing down your game.

1 Like

This is good to know! You can definitely use a combination of both parent+child classes and custom components (that is what I do in my game). It really depends how similar your actors/blueprints are going to be.

For example, my game has 16 enemy characters that all share most of the same functions and components (handling health, spawn location, physics, collision sphere, etc), but have different values for each and different skeletal meshes+animations. Making a parent class with 16 children makes sense here–I just need to go and change the default values to make 16 unique enemy’s that all handle the same; adding components individually would be tedious.

However, my parent enemy class also uses a custom component (a widget for a visible floating healthbar) that I reuse on other actors: it is the “progress bar” for buildings that are being built.

Parent+child classes are good for vertical design, and components/functions/tags are good for horizontal design. Both are useful!

This is the type of answer I was looking for as far as performance based and modular later down the ladder of game development, I’m still in my early phases of UE4 but this is a great initial piece of knowledge overall about components and why they can come in handy.