So, I’ve created a hierarchy where the base class constructor pretty much does all of the heavy lifting for creating objects using ConstructorHelpers.
Now, for subclasses, I want them to be able to set the paths to use for their specific assets before calling the whole heavy lifting parts in the parent constructor.
I tried creating my own virtual Init function, which is called at the start of the Base constructor to set some text references, and then I override this Init function in subclasses so they can set their own asset path references.
This does not work however, as it seems that only the base class Init function is ever called.
An example:
PWNWeapon.cpp - The Base class
// Constructor
APWNWeapon::APWNWeapon(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
// Set asset path for this specific class
SetConstructorRefs();
// Find and create arm mesh
static ConstructorHelpers::FObjectFinder<USkeletalMesh> armMeshObLeft(*ConstRefArmMeshLeftName);
if (armMeshObLeft.Object)
ArmMeshLeft = armMeshObLeft.Object;
}
void APWNWeapon::SetConstructorRefs()
{
ConstRefArmMeshLeftName = TEXT("SkeletalMesh'/Game/PWNGame/FPSArmsYYY/FPSArm_YYY.FPSArm_YYY'");
}
PWNWeapon_Melee - The child class:
// Constructor
APWNWeapon_Melee::APWNWeapon_Melee(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
// Set asset path for this specific class
// I assume that when the base constructor is called, it will call my version of SetConstructorRefs() to use my specific assets
}
void APWNWeapon_Melee::SetConstructorRefs()
{
ConstRefArmMeshLeftName = TEXT("SkeletalMesh'/Game/PWNGame/FPSArmsXXX/FPSArm_XXX.FPSArm_XXX'");
}
So what I’m hoping to achieve is to just have each subclass set their specific text references, and the base class constructor then uses these to construct the actual objects.
This approach does not work, so could anyone please give me a hint on what kind of pattern I can use to get this type of behaviour working?
Currently the only solution I see is to copy the whole base class constructor to the child class, which seems like a massive hack.
Another solution I thought of was to defer the construction of the objects, but then I can’t use ConstructorHelpers, as they can only be used in a constructor.
I’m really stuck on this one so any help would be appreciated.