Simplifying Code

Hi so I was wondering is there any way to simplify this code because I’m planning on adding more if / else if statements and I feel like it’s going to look quite messy. Maybe templates for the function could fix this, but I’m not sure if that would even work, any help is much appreciated, thank you :slight_smile:


void ATestingGameMode::SetNewGameState(const FString& GameStateName)
{
FActorSpawnParameters SpawnInfo;
SpawnInfo.Instigator = Instigator;
SpawnInfo.ObjectFlags |= RF_Transient;

if (GameStateName == "TestGameState")
{
ATestGameState* TestGameState = GetWorld()->SpawnActor<ATestGameState>(SpawnInfo);
GameState = TestGameState;
}
else if (GameStateName == "InGameState")
{
AInGameState* TestGameState = GetWorld()->SpawnActor<AInGameState>(SpawnInfo);
GameState = TestGameState;
}
}


I don’t know why you want to spawn the gamestate from code, when you can set the class in the gamemode, but anyways, you could send in the gamestate class directly via TSubclassOf<AGameStateBase> instead of a name.