Multiple inheritance

Hi to all of you

I’m currently working on some kind of RTS game. In this game, as you can imagine, there are buildings (AActors as simply as it can be) and ships (ACharacters for pathfinding, networking, etc.) and others elements but their are not important.
But my project was made in pure blueprint. But now I decided to switch to c++ and I met a problem. In ABuilding and AShip there are a lot of similar code such as meshes, health systems and other and other and I decided to make a ABase class which will hold code for those similarities. So I made like this: 1) ABase : public AActor - base class with most common code
2) ABuilding : public ABase - class for buildings, inherits from ABase
3) AShip : public ABase, public ACharacter - class for ships, the problem place, AShip should have not only properties as building but also capsule and pathfinding
I know that UE4 doesn’t support multiple inheritance nad I’m asking for advice what should I do. I found some solutions:

  1. Use Interface. But interface wouldn’t help with copying code in ABuilding and AShip when I will be writig implementation of a virtual methods
  2. Just rid off from ABase and use ABuilding and AShip. The problem is similar to (1)

Is it possible that you can wrap some of the common code inside an actor component? I haven’t seen your implementation so I cannot offer anything concrete. This was the approach that I took when implementing some of my systems. But it might not apply to your particular issue.

Most likely you are relying too heavily on the base types (ABase, ABuilding, AShip) and their added functionality.

You likely want to offload most the functionality into ActorComponents instead. You can do almost everything you need to inside them, and the very few rare things you can’t do directly inside them, you can reference the owning actor using GetOwner() and still stay inside the ActorComponents. There is a lot of tutorials/samples that really don’t push the Component mode nearly enough.

2 Likes

I’ve met the same issue as i wanna to create some stuff sharing among several inrelated classes, the way i worked out was:
1.set-up some derived component classes in order to make these common method work by only write once;
2.create these components under these classes
3.use some public interfaces to make an easy way to access these methods of the components

It worked well now, hope this could help :slight_smile:

**namrog84, **I even didn’t consider to put my code into AActor, thought that it will create mess in other actors. But now I see that it’s a plus one way to solve my problem. But what should I do in such situation: 1) I put my code in AActor
2) Now ABuilding inherits from AActor and AShip inherits from ACharacter which inherits from AActor, all works
3) Now I create ABullet, wich also inherits from AActor but don’t has some elements, such as health component (to remind, building and ship have this component). What should I do in this case? Should I just set HealthComponent to NULL? (I understand that I’m not providing you with code, but I think it’s simpler).
And I didn’t understand quite well what you meant here:

**wzspdw, **so you are saying to wrap my common code inside component and then add this component into other classes? But can I put multiple static meshes inside a component or can I put health component inside it? And could you explain this:

Why would I create interface if this component would still have code with setters/getters for example? Or I didn’t understand you well

1.Yes, of couse this can be done. Remember that the relationship between actors and components are not so strong, you might need to save a bunch of pointers to these static mesh components and other common code in your common components(or their inherited components). Just remember to do some create and attach to parent actor stuff in the components begin play code area.

2.Because there need to be some transportation work between AI controller, animation blueprint and the common components. Thats why I set my actors, interfaces and components like that .