Download

Change class hierarchy

If I have a class hierarchy like this:

AActor

AChildOfActor

AChildOfChildOfActor

AChildOfChildOfChildOfActor

… ]

How do I insert a new class in the middle without breaking all the generated code?

Can I just change the declaration of “AChildOfChildOfActor” to:


UCLASS()
class MY_GAME_API AChildOfChildOfActor : public ATheNewClass
{
    GENERATED_BODY()
...

or will that cause problems in the macros?

Well, through pure trial and error, I have something that works. I don’t know if what I’m doing is safe.

Let me simplify the class names so that my examples are easier to read. Suppose you had these classes, with the arrow pointing from derived to base:

AActor ← AA ← AB ← AC ← AD ← AE

And you wanted to change your hierarchy to this:

AActor ← AA ← AB ← ATheNewActor ← AC ← AD ← AE

This is working for me, so far, with no complaints from UE:

  1. In the editor, create the new class “ATheNewActor,” derived from AB.
  2. Close the editor so that it isn’t running while you do the rest of this.
  3. Open AC.h and change it’s parent, i.e.:

UCLASS()
class MY_GAME_API AC : public ATheNewActor
{
    GENERATED_BODY()
...

  1. Open the generated code for class AC (presumably “AC.generated.h”).
  2. Replace all occurrences of “AB” with “ATheNewActor.” I only see two places as of UE 4.7.
  3. Build and run the editor and it should now recognize class ATheNewActor as the parent of AC.

That is the usual way to re-parent a class, so it should work without any farther flaws.
Unless the UBT build tool messes something up, but its definitely a valid re-parent in C++ even though isn’t a common operation.

Yep, as TheOxStandard mentioned, sometimes UBT has a hickup. If that happens just perform a clean (or delete your entire intermediate directory) and rebuild. If that doesn’t work there is something else amiss like a missing simi-colon, a simi-colon where one should not belong, maybe something got deleted that should not have been, or a stray character from pounding your keyboard in frustration… But other than that yes, adding a new link in the inheritance chain is as simple as deriving your class from AB and making AC inherit from your new class (That much is standard C++/OOP).

Actually all you really need to do is Derrive your new class “MyNewClass” from AB. Then change AC to to use MyNewClass as it’s inherited class. A basic rebuild should work fine, if the UBT hickups then clean and/or delete your intermediate directory, but the next rebuild should work without issue. I’ve done this many times with no issue (other than my own mistakes such as accidently deleting the myclass.generated.h or one of the other previously mentioned problems…)