Initialize TArray with Blueprint

Hi guys,

I have a problem with TArray. I want to create an array of weapon to make an inventory of weapons.

Here’s my .h

TArray<TSubclassOf<class AWeapon>>Weapons;

And I tried initializing it like this

static ConstructorHelpers::FObjectFinder<UBlueprint>MyAssaultRifle(TEXT("Blueprint'/Game/FirstPerson/Blueprints/MyAssaultRifle'"));
    if (MyAssaultRifle.Object){
        UClass* weaponTemp = (UClass*)MyAssaultRifle.Object->GeneratedClass;
        Weapons.AddUnique(weaponTemp);
    }

After the constructor finish executed, the weapons array have the entry in it, but when the BeginPlay execute this code to spawn it

if (Weapons[0] != NULL){
        CurrentWeapon = GetWorld()->SpawnActor<AWeapon>(Weapons[0], GetActorLocation(), GetControlRotation());
    }

I have an error because the Weapons doesn’t have anything in it. I don’t get it. Someone could help me?

The initialization process for actors is pretty complex - post construction there is some property initialization done after constructors are called.

My suggestions are a) move the array initialization into begin play (store your class in a static) or b) move the array initialization into blueprint, where it is then easily customized.

Yeah, begin play would make sense to me too, but how do I retrieve the bluprint then? Because ConstructorHelpers is obviously not available outside the constructor, and I don’t know any other method to retrieve Blueprints.

Cache the UClass result in a static c++ variable accessible outside the function scope. I actually do most of mine in the AGameMode constructor and then set statics in any relevant classes that need to know about them.

Blueprints have the ability to get pointers to classes and assign actor variables. I’ve been moving towards this approach as I get more comfortable with blueprints (major logic in cpp, implementation details in blueprints is working quite well).

I’m gonna try this tonight and come back after! Thanks!