Download

Can I create a custom constructor?

I am recreating WoW like gameplay and I just finished creating my nameplates which is just text hovering over my gameobjects. Now I also created a Nameplate manager that manages all my nameplates.

I extended it from UActorComponent so that I could create it in my character constructor.


	Nameplates = PCIP.CreateDefaultSubobject<UTargetInfoManager>(this, TEXT("Nameplates"));
	Nameplates->owner = this;
	Nameplates->Init();

But as you can see I have to initialize my manager and it is quite error prone. What if I forget to initialize it?

So I tried to create a custom constructor like this


TSubobjectPtr<UTargetInfoManager> UTargetInfoManager::Create(const class FPostConstructInitializeProperties& PCIP,AMyProject3Character *owner)
{
	TSubobjectPtr<UTargetInfoManager> nameplates =  PCIP.CreateDefaultSubobject<UTargetInfoManager>(owner, TEXT("Nameplates"));
	nameplates->owner = owner;
	nameplates->Init();
	return nameplates;
}

The problem is that I can not assign a TSubobjectPtr anymore, I think I can only assign a TSubobjectPtrConstructor but I don’t think that I have access to it.

How did you create your custom constructor?

I dont think you can create custom constructors unless you work on source code to make it happen

Yes, because of the way the Unreal UObject system works, you cannot create your own constructors. You will need to create an initialization function that you call after creation to configure the object.

Okay thanks, did I make the right choice to subclass from UActorComponent? I first thought I should subclass directly from an Actor but I think an Actor should have a physical representation which I don’t need.