Character Deafult Constructor

Hi.

My project is a C++ First person shooter template. What im trying to do is take the character class that you get as a default and build functinality on to and around that character.

It all compiles and works fine untill i add an inheritence to this class. The child of the character has to have a reference as an inparameter in the constructor like this:

char.h


ATE_D_Character(const FObjectInitializer& ObjectInitializer, ATE_A_Character& inCharacter);

char.cpp


ATE_D_Character::ATE_D_Character(const FObjectInitializer& ObjectInitializer, ATE_A_Character& inCharacter) : Super(ObjectInitializer), mCharacter(inCharacter)
{
	
}

When you try to compile this it gives you an error saying: “You have to define ATE_D_Character::ATE_D_Character() or ATE_D_Character::ATE_D_Character(const FObjectInitializer&). This is required by UObject system to work correctly.”
So to fix this I define one of these two functions and this error is now gone. This kind of breaks the logic to the class but its not a very big deal.

However, I now have a constructor that does not take an ATE_A_Character Reference and that means that the reference I have as a member in the .h-file goes uninitilazied and the compiler throws an error at me saying it is uninitilazied. Is there anything I can do to be able to skip that constructor that does not have my reference in it? Why does UE4 need that specific constructor when the constructor I want to keep turns into this constructor down the line as the parent only has the constructor that UE4 wants?

I cant really use anything else than a reference there either, I’ts pretty crucial to the functionality i want.

Most of the automatic construction code in UE4 will call the standard ACharacter constructor : ACharacter (const FObjectInitializer& ObjectInitializer)

So even if your code inherits from ACharacter, it is still that constructor that will get called, not your new one. The only way around this would be to manually call that constructor yourself.

Alternatively (and this is what I do) - Add the reference to the class after it has been constructed. Ie, something along the lines of (I’m at work so this is only rough):



ATE_D_Character* MyChar = Spawn<ATE_D_Character>();
MyChar->mCharacter = InCharacter;


Oh, and don’t forget to set mCharacter =NULL; in your constructor to make it easier to debug!

Inheritance is evil. Use a component instead.