Just realised after watching the finishing up part 2 video that I misunderstood the point of making the mortars damageable - I was thinking we need to add the same health tracking and health bar UI to the mortars as for the tanks!
It strikes me that there are several possible solutions to doing this and I’d appreciate feedback on how to best do it using UE4’s mechanisms.
- Recreate the Mortars as C++ classes, inherit from them in the Mortar BP, make a separate copy of the healthbar UI that expects a Mortar C++ class as the entity to pull the health from etc. The core problem is that the HealthBar UI expects an ATank (which implements the contract of GetHealth) and so it’s fiddly to make this accept the Mortar as well.
- Create some kind of parent C++ class (e.g. ADamageableActor) that both the Mortar and Tanks inherit from, that implements the GetHealth contract, and have the HeathBar widget expect to use one of those. More along the lines of traditional C++ OOP design but I don’t like excessive hierarchies most of the time.
- Make some kind of UActorComponent that implements the health checking contract, put all of the health counters and GetHealth public function in there, then drop it onto both the Mortar and Tank objects. The problem now is that potentially you set the starting health on the tank and mortar, but that needs to be pulled into the health component at initialization. The HealthBar UI now needs to get a reference to the Health component, and we the wiring up of all the objects is a bit more fiddly.
- Just put all of the health counters into the Health UI widget, and have the Tank or Mortar update the UI counters directly whenever they take damage. They also need to check the counters in the widget to understand whether it is time to die or not.
I’ve been leaning towards the UActorComponent solution as it seems cleanest, although more fiddly. I’ve also started simple by creating the component as a blueprint, but now I’m puzzled as to how you can get a reference to it in the tank’s C++ class (i.e. to decrement the health counter on damage) - is that even possible? Or must you always use a C++ class in order to be able to access it directly?
It has been a bit of time since I touched this course so I’ve probably forgotten some basics - any ideas appreciated!