Help refactoring code

Evening All,

I am new to unreal and I come from a Java Development background. My question is about refactoring a Health System I am creating, I am sill yet to grasp the communication and how the components fit together.

I have Three Entities that make up the Health System: an Actor Component, a User Widget and the Third Person Character Blueprint.

The idea was to make this as modular as I can and try to keep to clean code principles, so my Actor Component has multiple functions to handle the relevant actions required in regards to health.

My Code:

So my main asks are:

  1. What logic/responsibilities should exists where?
  • For Example, The UI should control how it shows the state to the player, so having show health as a function in the Actor Component is not a good idea?
  1. What is the best way to refactor this code so it is truly modular and not coupled to the UI?

First off Player Health should be stored in the Player State class. Make a custom one for this. Health changes take place in the Player State on the server (authority).

The UI should have an event/function that sets the text for health. You’d call this when the Player State health float is modified. In a multiplayer setting this is done via RepNotify OnRep function.

Whenever you create a widget you should store a reference of it ( e.g. create widget: return → promote to variable ). You can then use that ref var to call events/functions in the widget.

I highly suggest creating a UI Manager class (Actor Component) to handle all your UI/Widgets. Your controller class should own this.

Stop using Get Player Controller. It isn’t reliable. Use Get Controller. This is used in the pawn class. Same applies to any node requiring and index.

Applying Damage should be done through the engines Apply Damage nodes.

Thank you @Rev0verDrive that’s a great start.

The Player State Class will this likely be another Actor Component that sits on the Character?

I am not working towards multiplayer as of yet, so updating the text, would that look something like?
UI > Custom Events (OnHeal)/(OnDamage)
AC (Player State) > ApplyHealth > calls OnHeal
AC (Player State) > ApplyDamage > calls OnDamage

I am struggling a little to understand the UI Manager Class, I cannot visualise what that would look like? Can you shed some more light on that please?

Player state is a core class that’s defined in the Game Mode (defaults).