How does Load Game from Slot work?

1- Does it create a save game object?

2- And on that note, do you need to manually destroy the save game object once you are done with saving the game, or is the object only temporary for your request

If you are loading thousands of games from slots it becomes meaningful knowing this.

So yeah, both of those questions.

1 Like
  1. If you are talking about LoadGameFromSlot from the UGameplayStatics library:
bool UGameplayStatics::LoadDataFromSlot(TArray<uint8>& OutSaveData, const FString& SlotName, const int32 UserIndex)
{
	ISaveGameSystem* SaveSystem = IPlatformFeaturesModule::Get().GetSaveGameSystem();
	// If we have a save system and a valid name..
	if (SaveSystem && (SlotName.Len() > 0))
	{
		if (SaveSystem->LoadGame(false, *SlotName, UserIndex, OutSaveData))
		{
			return true;
		}
	}

	// Clear buffer on a failed read
	OutSaveData.Reset();
	return false;
}

Normally that will use a generic save game system:

\UE_4.27\Engine\Source\Runtime\Engine\Public\SaveGameSystem.h

Which reads data from file and stores the relevant data to a property:

TArray<uint8>& OutSaveData

Some of the other methods work with USaveGame like LoadGameFromMemory.

  1. If you create a local property its memory will be managed automatically as long as you don’t use the “new” word to create it.

You should never have to do that. If you just want to know if a slot exists or not you can use the relevant methods on UGameplayStatics to check for them without loading any savegame. The only memory used of course are those of the classes you reference if the references are not soft.

3 Likes

But sometimes you do need to load thousands of games from slots, different slots, that do exist.
So, do yu happen to know if it creates a new object which must then be deleted? or is it only a way for you to access the actual .sav file in disk.

1 Like

It would help if you gave a few specific examples so I can give a more specific answer. Normally you don’t load more than a few USaveGame objects at all and those are used only temporarily. If you are wondering about showing thousands of save games in a list on the UI then no you don’t have to load everything in all save games to just display basic UI data for them.

(U)Objects are automatically garbage collected when nothing references them. You can work with USaveGame objects looping over your save game slots and forget about them afterwards. Properties of basic types will also be freed automatically if you don’t create them with the “new” keyword which we never really do.

Garbage Collection & Dynamic Memory Allocation - Old UE4 Wiki

Reddit - Dive into anything

Memory Management & Garbage Collection in Unreal Engine 5 | Mikelis' Game Blog

1 Like

@Roy_Wierer.Seda145
Hi, I did some tests and the objects continue to exist after the save game or load game is done.
I have tried checking after a whole minute, without promoting them to a variable or anything, and they still exist.
And destroy actor doesn’t seem to work on them, so I have no Idea how to get rid of these objects once they are created. And this is a BIG problem.

I suppose they must be referenced somewhere. If they were not referenced anywhere the Garbage Collector would automatically collect them and nullptr any pointers afterwards according to documentation. Garbage collector runs at configurable intervals, I believe the default is 30 seconds.

1 Like