Static UClass failed at 2nd launch in editor

Hi,

I’ve created an Static class to store all of my weapons meshes.
It allows me to create a lot of pawns set theirs weapons on the fly

so I have my Uclass :


UCLASS()
class MYPROJECT2_API AIWeapon : public AStaticMeshActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AIWeapon();

	UPROPERTY()
           TArray<UStaticMesh *> mStaticMeshs; // ARRAY OF WEAPON MESHES

	static AIWeapon* getAIWeaponInstance();

        void InitAll();

	
};


and the CPP with it :


AIWeapon::AIWeapon()
{
}

void AIWeapon::InitAll() {
	UE_LOG(LogClass, Warning, TEXT(" Init WEAPON 1 "));

	static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshOb_AW2(TEXT("StaticMesh'/Game/WEAPONS/SWORD.SWORD'"));
	mStaticMeshs.Add(StaticMeshOb_AW2.Object);

etc .....
}

AIWeapon* AIWeapon::getAIWeaponInstance() {
	static AIWeapon* IWEAPON;
	if (IWEAPON == nullptr || !IWEAPON->IsValidLowLevel())
	{
		UClass *IWEAPONclass = AIWeapon::StaticClass();
		IWEAPON = (AIWeapon*)ConstructObject<UObject>(IWEAPONclass);
    
                IWEAPON->InitAll();

    }
	return  IWEAPON;

}


so this here “works” good

When I game start I’m in the


MyGameMode



MyGameMode::MyGameMode()
{
    
    // first init of weaponInstance
    auto initBase = AIWeapon::getAIWeaponInstance();
    // first call and init of the meshes
}

and later on, every pawn wills call


    auto initBase = AIWeapon::getAIWeaponInstance();


and get theirs meshes perfectly.

BUT

Every time I launch again the project in the editor, the object


mStaticMeshs

became invalid (the


 mStaticMeshs.Num()

return -55675335…)

I think the problem is caused by because
the first init is done in the


MyGameMode

and after that every getAIWeaponInstance() are done in


beginPlay

Any idea ?

You’re using FConstructorHelpers outside of the constructor, which isn’t supported.

ConstructObject has been deprecated for over a year I think. You should be using NewObject.

And I’ve just noticed your class is an actor, which means you can’t use either, you have to use World->SpawnActor() to create actors. But I really don’t see why you want an actor just to preload all your meshes and store them. If you want a singleton object it shouldn’t be an actor, just a UObject.