I’m sure I’m making this more complicated than it needs to be, but I can’t seem to successfully fill a TSubclassOf array with the Blueprints inherited from my C++ class.
I’m working on a wave spawning system which spawns Actors derived from my AEnemy class.
I would like to search a folder in my project and load all of the derived Blueprints contained within, and add them to a TArray>.
Finally, I need to spawn these actors on demand.
This all sounds fairly simple, right? Well, apparently I’m also fairly simple, as I can’t seem to get this to work for me.
Below is the code in it’s current format which populates the array of Blueprints:
void UWaveManager::InitialiseEnemyList() {
FAssetRegistryModule* AssetRegistryModule = &FModuleManager::LoadModuleChecked<FAssetRegistryModule>(TEXT("AssetRegistry"));
TArray<FAssetData> AssetData;
FARFilter Filter;
Filter.ClassNames.Add(UBlueprint::StaticClass()->GetFName());
Filter.PackagePaths.Add("/Game/Blueprints/Enemies");
AssetRegistryModule->Get().GetAssets(Filter, AssetData);
int no = 0;
for (TArray<FAssetData>::TConstIterator PkgIter = AssetData.CreateConstIterator(); PkgIter; ++PkgIter)
{
FAssetData Asset = *PkgIter;
UBlueprint* BlueAsset = Cast<UBlueprint>(Asset.GetAsset());
if (BlueAsset->ParentClass == AEnemy::StaticClass()) {
//Do something
EnemyTypesArray.Add(BlueAsset->GetClass());
//TSubclassOf<AEnemy> FoundEnemy = *Cast<TSubclassOf<AEnemy>>(BlueAsset);
//TSubclassOf<AEnemy> FoundEnemy = *Cast<TSubclassOf<AEnemy>>(BlueAsset);
//EnemyTypesArray.Add(FoundEnemy->GetClass());
no++;
}
}
UE_LOG(LogTemp, Warning, TEXT("Number of BPs found = %d"), no);
UE_LOG(LogTemp, Warning, TEXT("Number of Enemy Type BPs added to array = %d"), EnemyTypesArray.Num());
}
Next is the temporary code used for spawning:
void UWaveManager::PerformWaveAction() {
FActorSpawnParameters SpawnParams;
// SpawnParams.Owner = this;
// SpawnParams.Instigator = this;
TSubclassOf<AEnemy> EnemyTypeToSpawn = EnemyTypesArray[0];
if (EnemyTypeToSpawn != NULL) {
AEnemy* NewEnemy = GetWorld()->SpawnActor<AEnemy>(EnemyTypeToSpawn);
if (NewEnemy) {
AliveEnemyArray.Add(NewEnemy);
Cast<AEnemy>(NewEnemy->EnemyDeathDelegate.AddDynamic(this, &UWaveManager::RegisterEnemyDeath));
}
}
else UE_LOG(LogTemp, Warning, TEXT("ActorToSpawn is Null"));
}
I’ve gotten so muddled with this, after trying again and again at different approaches, that I’m planning to start again in an attempt to spot and rectify any stupid little mistakes I’ve made. But in the meantime I would greatly appreciate it if someone else more able than I would be able to glance through the code and sight said mistakes on my behalf.
My understanding is that I need to convert the UBlueprint pointer to a Class type reference, but then store it in the subclass array. I should then be able to spawn by simply calling spawn actor, using the class in the array at the chosen index. The EnemyTypesArray does appear to be populated, but what with I’m not sure, as I am unable to spawn them.
Thanks in advance!