Serialization in blueprints

Dear Support

I’m using your Unreal Engine 4. I really like it. Thank you for your work!

I have a problem. I can not make saving and loading for my game. Please tell me, when will you add the serialization feature to the blueprints?

Hoping to hear from you

Have you checked out the following?

Yes. This function does not work as I need. This function saves each variable manually. I have many variables. Unfortunately, this function does not suit me.

I want to keep the current state level of all objects, then load this state.

Who can help me?

I would really like to have serialization in blueprints too.

In unity there is JsonUtility class. From that class, I can convert any object to JSON string and convert that string back to a new object or override the existing object.
It is a very useful feature. For example I can save the state of an object make some changes to it and ask it for something and if I do not like the answer I can just revert object state back with on click. (Check if inventory can add an array of items of different shapes, if not then revert back. Without it I would need to delete all the Added Items manually).
It is also useful when you need to save an array of different information in one place.
For example some generic save systems can be made. Save managers can have a dictionary with key and string-based data. Then you need to save managers notifies everyone who is interested in saving and they will add string-based save data and unique key names to an array. After that save manager can zip and save that single data in just one go. This will make save system independent and automatic. Without the need to MANUALLY saving each variable on each object, you want to save.

Currently your save system via save game is like one single mess.
Why do I have to duplicate every single variable and object into one save object manually?
Then connect each variable on save.
And then connect each variable on load.
And if you add new variables to the class which needs to be saved. You must not forget to create new variable in save game object, connect it in saving, connect it in loading… In above approach it will be handled automatically.
That is so much unnecessary time wasted.

After some research, I have found FArchive class that can handle serialization very well.
Could you please expose 3 functions to the blueprints:

  • ObjectToBinaryArray(); // Saves object with all variables marked with save attribute to binarry array
  • FromBinaryArrayToNewObject(); // Creates object of serialized type with serialized changed data
  • FromBinaryArrayToObjectOverride(); // Overrides an existing object with serialized data
    (either binarry array or json like string could be used)

That will be a great help to users.
Thanks in advance!

Exploring how the save game object is saved in Source code I found how to use Proxy Archive.
So I was able to expose 2 simple general-purpose save and load functions to blueprints.

h




UFUNCTION(BlueprintCallable)
static TArray<uint8> SaveToBinaryArray(UObject* Object);
UFUNCTION(BlueprintCallable)
static void LoadFromBinaryArray (TArray<uint8> SaveData, UObject* Object);



cpp




TArray<uint8> SomeSaveLoadClass::SaveToBinaryArray(UObject* Object)
{
TArray<uint8> SaveData;
FMemoryWriter MemoryWriter(SaveData, true);
FObjectAndNameAsStringProxyArchive Ar(MemoryWriter, false);
Object->Serialize(Ar); return SaveData;
}

void SomeSaveLoadClass::LoadFromBinaryArray(const TArray<uint8> SaveData, UObject* Object)
{
FMemoryReader MemoryReader(SaveData, true);
FObjectAndNameAsStringProxyArchive Ar(MemoryReader, false);
Object->Serialize(Ar);
}



This 2 simple functions can solve many different problems with automatic saving state of Any UObject.
Just call the function and all properties will be saved into binary array.
You than can do with it everythink you want. Test somethink and revert back. Clone Object. Save persistant state of object and write it in file.

This is similar to how JsonUtility.ToJson and FromJsonOverride works in Unity.

Saving thousands of objects is instant. Loading takes a little bit of time.

I find this functions very helpful and would like to have them In Engine for everyone.

Is it worth it trying to make a pull request with it? Or I should just create free plugin?

@TextusGames how have you dealt with object pointers. What if your objects has a UPROPERTY that is a pointer to another object/actor in your map? Or even a TArray of AActor or similar? Curious to know how you deal with that.

In your process, have you experienced any weird crashes before getting it to work? I’m trying to use the FObjectAndNameAsStringProxyArchive methodology like you as well, and although it works for an incredibly simple demo project (that I built for practicing), using it in a professional game project, it just doesn’t work; throws up obscure crash errors about memory access violations and similar.