How to load GameState data from a save file?

II_ADN_II
Fri, 22 Sep 2023 14:56:14 +0000
Programming & Scripting

Hi, I was trying to save game state variables but found out that the game state gets constructed AFTER loading (loading happens before beginPlay) so its not loading with the rest of the actors. This seems to be by design as far as I know.

I tested loading again the gameState data from the GameState BeginPlay but somehow the data is not assigned. I am sure the data gets saved, seems to be a problem with restoring the data.

I followed TomLoomans guide for the save/Load structure.

What is the correct way of loading data? Is saving the GameState a bad practice?

EDIT:

Ended up loading like so in case anyone have the same problem:

// Here I saved the MapName and Options to use them later
void AMyGameMode::InitGame(const FString& MapName, const FString& Options, FString& ErrorMessage)
{
	Super::InitGame(MapName, Options, ErrorMessage);

	InitGameMapName = MapName;
	InitGameOptions = Options;

}


void AMyGameMode::PreInitializeComponents()
{

	Super::PreInitializeComponents();

	//Notify saveGame subsystem in case we want to load a saveGame
	const UGameInstance* GI = GetGameInstance();
	if (GI != nullptr) {
		USSaveGameSubsystem* SG = GI->GetSubsystem<USSaveGameSubsystem>();
		if (SG != nullptr && HasAuthority()) { SG->OnPreStartGame(InitGameMapName, InitGameOptions); }
	}

}

EDIT:

Game state name changes, not sure why but its not reliable to use the name as a loading identifier.

6 posts - 3 participants