Download

Converting data-table row names to class name to spawn actors

Hello, so I’m working on a squad-based strategy game like Company of Heroes. I am currently still building a squad framework that the player will interface with and give orders to. I have decided to use data tables to define the contents of squads and units and have run into trouble figuring out how my CreateUnit function will work (which will handle spawning soldiers on the squad actor’s request). I pass two parameters to it, namely the requested class name in the form of an FName taken from the row name of the data table and the amount of that type that must be spawned. I have run into trouble trying to translate that FName variable into a classname reference for use with the SpawnActor function. This is a slimmed-down version of the relevant code:


TArray<FName> ClassNames = ContainerDataTable->GetRowNames();

// Call CreateUnit function for every request until squad is full
for (auto& RequestedUnitClass : ClassNames)
{
// Select Row
FUnitContainerData* SelectedRow = ContainerDataTable->FindRow<FUnitContainerData>(RequestedUnitClass, "");

// Find number requested
int AmountToSpawn = SelectedRow->NumberOfUnits;

CreateUnit(RequestedUnitClass, AmountToSpawn);
}

void AUnitContainer::CreateUnit(FName Classname, int Amount)
{
// Determine class from row name
FName RequestedUnitClass = Classname;

// Spawn unit from Class
FActorSpawnParameters SpawnInfo;
RequestedUnitClass* Unit = GetWorld()->SpawnActor<RequestedUnitClass>(RequestedUnitClass::StaticClass(), SpawnLocation, SpawnRotation, SpawnInfo);

The Issue I’m having is that the variable RequestedUnitClass is not accepted as part of the arguments of the SpawnActor function. I’m still learning C++ and it seems to me that I am simply passing on the wrong data. I would like to use Row names as variables for class names but is that even possible? Like a row named SoldierType1 would spawn the class named SoldierType1 when parsed, with corresponding data (in this case, how many need to be spawned). What am I missing here to complete this translation?