Home > database >  Why delegated constructor doesn't work as expected UE4
Why delegated constructor doesn't work as expected UE4

Time:11-01

I am having issues with loading the asset for several weapons in the game, such as AK 47, M11, and so on, the issue here is that I created a c class for doing that work which will be

    header
    
    UCLASS()
    AWeapon_core : public AActor
    {
        private: 
        USkeletalMeshComponent* m_skeletal_mesh;

        public:
        AWeapon_core(FString);
    
        protected:
        const FString mk_default_path;  
    };
    
    cpp
    
    AWeapon_core::AWeapon_core(FString _path)
    {
        m_skeletal_mesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Weapon_mesh"));
    static ConstructorHelpers::FObjectFinder<USkeletalMesh> WEAPON_MESH(*(mk_weapon_mesh_path   _path));

    if (WEAPON_MESH.Succeeded())
        m_skeletal_mesh->SetSkeletalMesh(WEAPON_MESH.Object);
    }

And for the weapons, for example AK-47

    header

class AAK47 : public AWeapon_core
{
    public:
    AAK47();
}

cpp

AAK47::AAK47() : AWeapon_core("The path of the AK47 mesh file")
{
}

Did the same for all of the others weapons but it creates with the same mesh, what could be wrong here?

CodePudding user response:

Do you realize what you did here?

static ConstructorHelpers::FObjectFinder<USkeletalMesh> 
                       WEAPON_MESH(*(mk_weapon_mesh_path   _path));

It's a STATIC LOCAL variable. It is initialized once. Only once as there is only one instance of AWeapon_core::AWeapon_core() function. All subsequent call to this constructor would use already initialized value.

  • Related