# What is the best way to populate USaveGame on LoadGame before serialization?

**URL:** <https://forums.unrealengine.com/t/what-is-the-best-way-to-populate-usavegame-on-loadgame-before-serialization/464919>\
**Category:** Editor Scripting\
**Tags:** savegame, loading, unreal-engine, question\
**Created:** [May 11, 2020, 6:44am UTC](https://forums.unrealengine.com/t/what-is-the-best-way-to-populate-usavegame-on-loadgame-before-serialization/464919 "2020-05-11T06:44:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nansP](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@nansP](https://forums.unrealengine.com/u/nansP)\
**Post date:** [May 11, 2020, 6:44am UTC](https://forums.unrealengine.com/t/what-is-the-best-way-to-populate-usavegame-on-loadgame-before-serialization/464919/1 "2020-05-11T06:44:23Z")

</div>

Hi there,

First, sorry for my english, I try to do my best but i’m not fluent at all.

To give you more details:

##My Needs##

I need to populate my derived `USaveGame` object with an `UObject` containing the current `UWorld` (as a WorldContextObject) on the Load Game process **BEFORE** serialization.

This to allow my `USaveGame` object to use current world to spawn actors as suggest [here](https://answers.unrealengine.com/questions/35618/savingloading-an-array-of-objects.html?sort=oldest) (the response by [Ben Zeigler](https://answers.unrealengine.com/users/173/view.html)) or retrieve some objects from `UGameInstance`.

##problems##

I try to use the native **Save Game System** which has been improve since by [Ben Zeigler](https://answers.unrealengine.com/users/173/view.html) and Mike Beach (2 years ago) to avoid re-coding my own system.

The thing is I can’t do that with this new system, in the[`UGameplayStatics::LoadGameFromMemory()`](https://github.com/EpicGames/UnrealEngine/blob/42d84f7d1f9c2147ad109179f268fd1542b2ccd2/Engine/Source/Runtime/Engine/Private/GameplayStatics.cpp#L2102) we can see 2 things which prevents me to do that:

- the `USaveGame` is instanciated with the `GetTransientPackage()`, so no current `UWorld` in it
- [USaveGame::Serialize()](https://github.com/EpicGames/UnrealEngine/blob/42d84f7d1f9c2147ad109179f268fd1542b2ccd2/Engine/Source/Runtime/Engine/Private/GameplayStatics.cpp#L2105) is called directly after instanciation.

"

## Solutions?

1. My first attempt was to create my own loading function, I just had to copy/paste [UGameplayStatics::LoadGameFromMemory()](https://github.com/EpicGames/UnrealEngine/blob/42d84f7d1f9c2147ad109179f268fd1542b2ccd2/Engine/Source/Runtime/Engine/Private/GameplayStatics.cpp#L2077) and populated my `UsaveGame` object before serialization with whatever I want. I was surprised when I did it cause the function use `FSaveGameHeader` which is declared in the CPP files, so I couldn’t re-use it as such, and i had to copy it with its other dependencies: `UE4_SAVEGAME_FILE_TYPE_TAG` static int and `FSaveGameFileVersion`…  
Do you know why these are maintain private in this context? Do you have any warning to put this in the `.h` file?

2. My second though was to think about how to make the actual code more extensible. I’m guessing that passing an optional delegate as parameter of this function should do the trick like:

In [Engine\Source\Runtime\Engine\Classes\Kismet\GameplayStatics.h](https://github.com/EpicGames/UnrealEngine/blob/4.25/Engine/Source/Runtime/Engine/Classes/Kismet/GameplayStatics.h)

```
DECLARE_DELEGATE_OneParam(FLoadSaveGameObjectDelegate, USaveGame*);

```

In [Engine\Source\Runtime\Engine\Private\GameplayStatics.cpp](https://github.com/EpicGames/UnrealEngine/blob/4.25/Engine/Source/Runtime/Engine/Private/GameplayStatics.cpp#L2077)

```
// line 2077
LoadGameFromMemory(const TArray<uint8>& InSaveData, FLoadSaveGameObjectDelegate const& InDelegate)  
// line 2103  
if (InDelegate.IsBound())  
{
   InDelegate.Execute(OutSaveGameObject)
}

```

This implies additional codes to make the Delegate optionnable (can be inspired from `FTimerUnifiedDelegate`) but you get the idea.

## Questions

So questions are:

- Am I in a complete wrong direction 😃 and there is an already better solution which fit my needs?
- If not, do you think my solutions worth to be in the engine code?  
(put `FSaveGameFileVersion` , `UE4_SAVEGAME_FILE_TYPE_TAG` and `FSaveGameHeader` declaration in the `.h` **OR** add a Delegate in GameplayStatics::LoadGameFromMemory() method)

Thanks, I hope i’m understable enough 😃

---

<div class="post-metadata">

**Author:** ![archimonde1111](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@archimonde1111](https://forums.unrealengine.com/u/archimonde1111)\
**Post date:** [February 12, 2024, 1:48pm UTC](https://forums.unrealengine.com/t/what-is-the-best-way-to-populate-usavegame-on-loadgame-before-serialization/464919/2 "2024-02-12T13:48:43Z")

</div>

Hi, it’s an old topic but did you solve the issue?
