What is saved when saving Pointers in Savegame Objects

Hi Everyone,

I made a Savegame Object with a Variable TMap<AActor*, bool> Doors. So I basically want to save if the Door was opened or not already.

I have Main Menu where I can choose a Save Slot and start the game. The on clicked event of the Main Menu Loads the Savegame Object and therefore also the TMap. Then I open the level and set the actors data from the savegame.

In the Level Blueprint in BeginPlay() I have the following code:

TArray<AActor*> OutDoors;
Doors = Cast<UMyGameInstance>(GetGameInstance())->SaveGameData->Doors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), ADoor::StaticClass(), OutDoors);
	for (AActor* OutDoor : OutDoors)
	{
		TArray<ADoor*> KeyDoors;
		Doors.GetKeys(KeyDoors);
		ADoor* CastDoor = Cast<ADoor>(OutDoor);
		if (CastDoor)
		{
			if (Doors.Find(CastDoor))
			{
				CastDoor->IsOpen = *Doors.Find(Cast<ADoor>(OutDoor));
				CastDoor->UpdateDoorLocation();
			}
		}
	}

The Problem is on the place with (Doors.Find(CastDoor)) line. It seems that the Doors at this point only contains nullpointers.

That leads me to my first Question:

What is actually saved here? Because if I save a pointer, it just would be a memory adress right? Or is the Object serialized in some way automatically?

The second thing, that confuses me is, if I load the Savegame again in the Level Blueprint in the first line of BeginPlay() like this:

UMyGameInstance* GameInst = Cast<UMyGameInstance>(GetGameInstance());
GameInst->LoadGame(GameInst->SaveSlotName);

Then the Doors.Find actually has a Result and everything works fine. Why is that?

Thanks for the answers.

It saves string based references, that should look like PersistentLevel:Door_42.

When you load savegame, it tries to resolve the references, and put nullptr if matching objects are not found.

Since you are loading savegame before loading level, all your refs are nullptr.
When you load savegame after loading level, references can actually resolve to objects in the level.
Note that if you were referencing dynamically spawned objects (not placed in the level) it probably would not work well unless they’re spawned in a very consistent way.

2 Likes