I can’t figure out how to spawn an actor by it’s enum key in TMap (the last line of code). Here’s my setup:
// IN .H FILE
//Enum keys to be exposed in blueprint
UENUM(BlueprintType)
namespace EObjectType
{
enum Type
{
STATIC UMETA(DisplayName = "Static Object"),
MOVABLE UMETA(DisplayName = "Movable Object"),
// More types
};
}
// Struct to assign object type to an actor in editor
USTRUCT(BlueprintType)
struct FObjectActor
{
GENERATED_USTRUCT_BODY()
// Type of the game object
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Game Object Types")
TEnumAsByte<EObjectType::Type> ObjectType;
// The blueprint for a game object
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Game Object Types")
TSubclassOf<class AActor> ObjectActor;
};
// I create an array that will hold all the Object Actors
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Game Object Types")
TArray<FObjectActor> ObjectActors;
// I create a TMap which will assign object actors to object types
UPROPERTY()
TMap<TEnumAsByte<EObjectType::Type>, TSubclassOf<class AActor>> ObjectLibrary;
// IN .CPP FILE
// I copy items from ObjectActors array into the TMap
for (int i = 0; i < ObjectActors.Num(); i++) {
if (!ObjectLibrary.Contains(ObjectActors[i].ObjectType)) {
ObjectLibrary.Add(ObjectActors[i].ObjectType, ObjectActors[i].ObjectActor);
}
}
// Now I need to spawn the actor with enum key STATIC
// Set the spawn parameters.
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this->GetOwner();
SpawnParams.Instigator = NULL;
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
// Spawn the actor
AActor* NewObject = GetWorld()->SpawnActor<AActor>(ObjectLibrary?????????????, FVector(0, 0, 0), FRotator::ZeroRotator, SpawnParams);