# How to load GameState data from a save file?

**URL:** <https://forums.unrealengine.com/t/how-to-load-gamestate-data-from-a-save-file/1307892>\
**Category:** Programming & Scripting\
**Tags:** savegame, gamestate, unreal-engine, CPP, question\
**Created:** [September 22, 2023, 2:56pm UTC](https://forums.unrealengine.com/t/how-to-load-gamestate-data-from-a-save-file/1307892 "2023-09-22T14:56:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![II\_ADN\_II](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ii_adn_ii/32/1194656_2.png) [@II\_ADN\_II](https://forums.unrealengine.com/u/II_ADN_II)\
**Post date:** [September 22, 2023, 2:56pm UTC](https://forums.unrealengine.com/t/how-to-load-gamestate-data-from-a-save-file/1307892/1 "2023-09-22T14:56:15Z")

</div>

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](https://www.tomlooman.com/unreal-engine-cpp-save-system/) 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:

```auto
// 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.

---

<div class="post-metadata">

**Author:** ![xenofsnstudios](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xenofsnstudios/32/1194578_2.png) [@xenofsnstudios](https://forums.unrealengine.com/u/xenofsnstudios)\
**Post date:** [September 23, 2023, 12:30pm UTC](https://forums.unrealengine.com/t/how-to-load-gamestate-data-from-a-save-file/1307892/2 "2023-09-23T12:30:52Z")

</div>

I found that the `BeginPlay` functions are all non-deterministic, but there is an event that is fired from `UWorld` when all of the `BeginPlay` functions have been fired, so I hook into that to load my stuff.

```auto
GetWorld()->OnWorldBeginPlay.AddUObject(this, &AYourCoolActor::InitLevel);

```

---

<div class="post-metadata">

**Author:** ![II\_ADN\_II](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ii_adn_ii/32/1194656_2.png) [@II\_ADN\_II](https://forums.unrealengine.com/u/II_ADN_II)\
**Post date:** [September 23, 2023, 2:27pm UTC](https://forums.unrealengine.com/t/how-to-load-gamestate-data-from-a-save-file/1307892/3 "2023-09-23T14:27:30Z")

</div>

But the load should be done before BeginPlay, otherwise it may lead to incorrect initialization.

Still, its a useful trick for general initialization.

---

<div class="post-metadata">

**Author:** ![xenofsnstudios](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xenofsnstudios/32/1194578_2.png) [@xenofsnstudios](https://forums.unrealengine.com/u/xenofsnstudios)\
**Post date:** [September 23, 2023, 2:43pm UTC](https://forums.unrealengine.com/t/how-to-load-gamestate-data-from-a-save-file/1307892/4 "2023-09-23T14:43:04Z")

</div>

I guess that depends on what you are loading. “GameState variables” could mean anything. You weren’t clear on what you needed to set up.

If you want to set something up before the `BeginPlay` functions are called, there are many different functions that get called before that, including:

• `GameInstance.InitializeForPlayInEditor`  
• `GameInstance.Init`  
• `GameInstance.StartPlayInEditorGameInstance`  
• `GameInstance.OnStart`  
• `GameModeBase.InitGame`  
• `GameModeBase.PreInitializeComponents`

These get called before `GameModeBase.StartPlay` which calls all the `BeginPlay` functions of the actors in the world.

---

<div class="post-metadata">

**Author:** ![II\_ADN\_II](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ii_adn_ii/32/1194656_2.png) [@II\_ADN\_II](https://forums.unrealengine.com/u/II_ADN_II)\
**Post date:** [September 23, 2023, 3:04pm UTC](https://forums.unrealengine.com/t/how-to-load-gamestate-data-from-a-save-file/1307892/5 "2023-09-23T15:04:40Z")

</div>

Sorry I meant to load saved data from a file. Used InitGame, but since GameState is created after I had to load on GameMode::StartPlay() before Super::StartPlay().

It was the closest to InitGame that I could find but as you said PreInitializeComponents runs before so Its probably a more appropiate place for loading. Ended up loading like so in case anyone have the same problem:

```auto
// 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); }
	}
}

```

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [October 23, 2023, 3:05pm UTC](https://forums.unrealengine.com/t/how-to-load-gamestate-data-from-a-save-file/1307892/6 "2023-10-23T15:05:33Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
