Inheritance or Actor Component about... Player/Enemy Units and C++ UMG by WCode

It may sounds silly question… but I really need your experience and advice about it.

As I said in previous posts, that I am developing single player 2D Chrono Trigger-ish (or old Final Fantasy) RPG.

I face beginner problems about Inheritance.


When I was in Unity (Yeah…) C# Programming, my colleague commented once that

“Inheritance is good too, but implementing Player/Enemy by components might be better in future.”

Normally I would inherit the base character class to implement various units - Player and enemies.

When I encounter Actor Component… it makes me to tempt to try different approach.

I planned Actor class (base character class) with Actor Components (such as AI, Stat, PlayerController?).

But it was once failed by TickComponent which didn’t work out well (didn’t work out).

However, I face another problem, which is critical… even if I solve TickComponent problem.

How do I access Actor Component’s variables (such as HP, Action, e.t.c.)?

In Unity, normally I used “GetComponent<TheComponentType>” to approach the component in objects.

In Unreal Engine, is there? I assume there would be, but I don’t know how.

Back to the question, should I use Inheritance or Actor Component about types of Units?

C++ UMG Inventory Widget class by WCode (I think it is him)…

How can I expand it with C++? Can anyone gives me good example?

I thought about it as usage for showing available items to use…

The problem is… all items are from TArray or TMap, is it possible to show all items as Widgets(actually Fonts with Images) from TArray or TMap?

I am going to research on UMG soon, but it would be better if anyone has experience on UMG or HUD to advise me.

I apologize for speaking from Unity’s aspect, and sounds complicated, but I really am desperate with programming and time is ticking again.

Please Help me T.T

Here is something to consider when using inheritance vs components:
Components can be attached and dettached during runtime to any actor possible. It does not matter what specific class you are attaching the component to as long as it inherits from AActor at some point. This is very powerful as you can add and remove behaviour to any actor no matter from what they inherit at any times you wish. This makes your project more maintable as if you notice that some actor needs certain behaviour you can just add the component. This is way most if not all code that is sold to third-parties, such as on the marketplace, will provide components for behaviour and not actor classes implementing the same behaviour.
Inheritance on the other hand forces your client, meaning the user of the code, which may only be you for now but could expand to more people as the project grows, to use a specific interface and is fixed at compile-time. This might cause a problem when you identify that some behaviour that you decided will be inherited must be added to a class that already has a different inheritance hierarchy.

I think this scenario shows the point I am trying to make best and also fits you situation; you mentoined an inventory so let’s start on that. Consider you want an actor to have an inventory storing items. You have two options: Create an inventory component called UInventoryComponent, which is attached to any actor which can have an inventory, or make a base class AInventoryActor from which all actors must inherit to have an inventory. Let’s go with the second option for now: Consider you create a character inheriting from AInventoryActor. All fine it has an inventory, all good. Now say you have another class that you created at some point, let’s say ACar, and it directly inherits from some other custom class that you wrote earlier. You notice that cars should also have inventories but changing its parent to AInventoryActor is not an option as your ACar class requires the your other custom class to function. Now you have a problem: While in option 1 you could have simply added a UInventoryComponent and be done with it, with option 2 you must reconsider the class hierarchies and apply potentially large changes to them which are in my experience a great source of error.

On resume? Prefer dynamic options, such as components, to static options, such as inheritance. Inheritance is a great feature in object-oriented programming but it is misused in 90% of the cases in my honst opinion.

To your implementation question about components: You can get an array of all components in an actor by using AActor::GetComponents (see here AActor::GetComponents | Unreal Engine Documentation). You can even specify a base class to fitler the search as template argument.

I think the best way is to blend intelligently both (inheritance and actor component), for example make an Enemy pawn with an Stats component and some C++/Blueprint code, inherit this one to make a more complexe Enemy, and you can reuse your Stats component on your PlayerState of your character, abstract inheritance is cool for this :slight_smile:

If you want to check if your Actor has this Actor Component, simply create an interface, make some functions like “HasMyAwesomeComponent” and return true or false, I see some people using this, you can also use <T*>FindComponentByClass().

You can easily extends Widget C++ class like any other class, and I think this playlist is nice to start UMG creation : UI Inventory Tutorial (pre. v4.7) - YouTube

If all your data is on GameInstance in a TMap or TArray, you simply need to work with ID, imagine you have an array of items as Inventory, if the first Item has ID 75, get your data from your TMap/TArray and bind what you want to your inventory slot.

I hope it help you ! :slight_smile:

Wow Thank you so much! You cleared my thought.

thx for inventory ideas!