My two cents…
We want to have spawn points in our level, that randomly pick from a category. In order to keep the definition in the level light-weight we use the string of the loot
like…
Blueprint'/Game/Blueprints/DevelopmentObjects/TestActor.TestActor'
So we have a LootSpawinPointBP, a LootChoiceBP blueprints.
The LootChoiceBP is a string reference to a blueprint, and a weight. The weight is a relative random selection weight.
There is also a second bleuprint refence for ammo so a spawned gun can have some ammo next to it.
We then make blueprints from the base blueprints for a specific spawn point category.
For example we might have a UpstairsClosetLootSpawnBP that is a LootSpawinPointBP with a bunch of LootChoiceBP like Rifle308LootBP, BackpackRedLargeBP and such.
On startup the loot spawn point randomly chooses on of the LootChoiceBP with the higher weight items being more likely to get chosen.
(You can also have a choice NoChoiceBP with both blueprint ref strings empty too.)
So this requires that we can spawn from a string. Thus the code that counts…
AActor * UtdlBlueprintHelpers::SpawnActorFromReferenceString(AActor * sourceActor, FTransform trans, FString blueprintRefString, FName nameToAssign)
{
//UE_LOG(TDLLog, Log, TEXT("UtdlBlueprintHelpers::SpawnActorFromReferenceString %s"), *blueprintRefString);
if (blueprintRefString.Len() == 0)
{
return NULL;
}
AActor * a = NULL;
FStringAssetReference itemRef = blueprintRefString;
if (itemRef.TryLoad() != NULL)
{
UObject* itemObj = itemRef.ResolveObject();
if (itemObj == NULL)
{
UE_LOG(TDLLog, Log, TEXT("UtdlBlueprintHelpers::SpawnActorFromReferenceString invalid blueprint reference (resolve falied): %s"), *itemRef.AssetLongPathname);
return NULL;
}
UBlueprint* gen = Cast<UBlueprint>(itemObj);
if (gen == NULL)
{
UE_LOG(TDLLog, Log, TEXT("UtdlBlueprintHelpers::SpawnActorFromReferenceString invalid blueprint reference (cast failed): %s"), *itemRef.AssetLongPathname);
return NULL;
}
FActorSpawnParameters SpawnInfo;
SpawnInfo.bNoFail = true;
SpawnInfo.bNoCollisionFail = true;
SpawnInfo.bRemoteOwned = false;
if (nameToAssign.IsValid() && nameToAssign.ToString().Len() > 0)
{
SpawnInfo.Name = nameToAssign;
}
a = sourceActor->GetWorld()->SpawnActor<AActor>(gen->GeneratedClass, SpawnInfo);
if (a == NULL)
{
UE_LOG(TDLLog, Log, TEXT("UtdlBlueprintHelpers::SpawnActorFromReferenceString invalid blueprint reference (spawn failed): %s"), *itemRef.AssetLongPathname);
return NULL;
}
a->SetActorTransform(trans);
}
return a;
}