Properly formatting subclass constructors to use ObjectInitializer

Sorry if I’m being ignorant, but what do you need an object initializer for at all? I started using UE4 a little over a year ago and never needed to implement a constructor with that parameter. I can still create “default subobjects” in my actor-classes.

no problem :slight_smile:

I have an ABaseCharacter class that all humanoids are children off, and tons of individual subclasses for playable characters, cutscene actors, and so forth. I’m trying to get them to all use FObjectInitializer, so I can instantiate some components on each character in their constructor. My base class’s constructor dec is as follows:

//.h
ABaseCharacter(const class FObjectInitializer& ObjectInitializer);

//.cpp
ABaseCharacter::ABaseCharacter(const class FObjectInitializer& ObjectInitializer)
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	//PrimaryActorTick.bCanEverTick = true;
	myFlipbook = ObjectInitializer.CreateDefaultSubobject<UPaperFlipbookComponent>(this, TEXT("My Flipbook"));
	InitializeMeshes();
}

The constructor of each child class is then a class-specific variant of the following:

//.h
ACutsceneNPC(const class FObjectInitializer& ObjectInitializer);

//.cpp
ACutsceneNPC::ACutsceneNPC(const class FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	myCutsceneRelay = CreateDefaultSubobject<UCutsceneRelay>(TEXT("Cutscene Relay"));
}

This looks right to me; I’m adding ObjectInitializer to the declaration and definition of my base constructor, then including it in each child class, along with : Super(ObjectInitializer) . However, that generates an error (overloaded member function not found in ACutsceneNPC), and removing the : Super bit from the .cpp produces a separate error, “no appropriate default constructor available”. Is the implication that I need to keep my base class’s constructor virtual and only instantiate components in child classes, or is there something just plain doofy about my syntax?

Huh, that’s really weird; in my head you always needed to use objectinitializer, I don’t know if it changed over time or I was just being a dummy but you’re absolutely right; modifying the function call to just CreateDefaultSubObject(TEXT(“Name”)) works perfectly; thank you!