How to properly handle inheritance changes?

Hello,

Say I have a pawn class Player (extending pawn) and after some time I decide to add an Enemy (also extending pawn). Then I notice, they have many common qualities and I could use some abstraction, so I’d like to create a parent class.

What is the right way to do it if…
A) I want the parent class to be just an interface that cannot be instantiated?
… I would probably just create raw c++ header, but I am not sure if I wanted to use features like UFUNCTION it is the right thing to do.

B) The parent can be instantiated (say it is another pawn type).
… Would I break something if I just change the parent class? How about the generated code?

If you declare any pure virtual methods in a parent class it cannot be instantiated. (make a virtual method “=0”). You can also have friend classes and multiple inheritance - creating smaller classes for the shared code which can be reused between different classes:

class firstClass {
public:
	int32 firstVar;
	virtual int32 firstMethod()=0;
	int32 firstClassTask() { return 32; }
};

class secondClass {
public:
	int32 secondVar;
	virtual int32 secondMethod()=0;
	int32 secondClassTask() { return 42; }
};

class multiClass : public UObject,public firstClass,public secondClass {
public:
	int32 multiVar;
	virtual int32 firstMethod() { return firstVar+secondClassTask(); }
	virtual int32 secondMethod() { return secondVar+firstClassTask(); }
};

Yes, that is what I meant by the raw c++, but is it ok to declare the pure virtusl function as UFUNCTION() for example?

Kind of, check out this thread:

1 Like

Hmm that’s nice, I have noticed the examples with the abstract uclass derive from some Uobject, is it necessary for creating UFUNCTIONs? If yes, what do I do about the Diamond problem (A extends P, B extends P, C extends A & B)?

Yes you’ll need the parent to be derived from UObject and the children cannot have other UObject based classes multiple-inherited - but if you create child classes derived from the parent they can have UFUNCTIONS etc and you can use those children as parents to your practical child classes - if that makes sense…

1 Like

Yes, I have figured out the way to do it is probably using UINTERFACE, which unfortunately cannot directly implement any functions, but it provides sufficient abstraction.

Also, I have tried just changing the parent (class A : public APawn to class A : public MyNewParent), which pretty quickly crashed the engine, but after a clean rebuild - which I assume regenerated the files - it seems to be fine.

1 Like