Saving Object-References/Pointers in a SaveGame?

Is there a standardized way to save/serialize a UPROPERTY()-pointer to another object?

Since the UE4 editor obviously handles references on an engine-level and makes sure they persist throughout the sessions, I thought there may be something I can use.

Or if there isn’t, how would you deal with it? Do I need to save IDs to file and initialize all pointers manually again after loading or is there a better way after all?

did you find a solution?

No, sorry. I haven’t been dealing with save-games in a long while, and I won’t try to save scenes actor by actor (including references) anymore.

Since nobody has posted an answer in more than half a year, I guess you would have to do it manually by assigning an ID-value to every referencible actor/object right before saving the game, then save references as ID value of the referenced object and save each object’s ID along with the rest of its data. When loading, create objects/actors from save-data with their saved ID and when all objects have been created, iterate through them one more time to restore their references by finding the remembered IDs and their corresponding actors.

This is probably trivial to you. Anyone can do that. I was just wondering, if UE4 has an implementation for this sort of thing out-of-the-box.

edit: btw, you can also use each object’s memory-adress as ID, because it’s always unique and it saves some “conversion”-effort. It could make sorting a little harder though, if you want to directly access pointers by ID through an array in order to avoid container-search…

1 Like

Ok the solution you said, recreating each object, and iterating through them one more time to restore reference is what i did in a c++ test project.

this is the best solution i found too but i am sure there is something am missing, it should be easier to save :frowning:

Hi Guys, I was struggling with this for a bit and still learning best way to do it. I’ve created a wiki page showing what I’ve setup so far if it might help with your setup in anyway, adding to this Answer Hub question because it’s one of the questions when googling save game and pointers as I was when doing when trying to figure it out :slight_smile:

It’s my first tutorial so feel free to offer any suggestions for updating the page or clarify anything.

I know this thread is old, but I wanted to give my input here. You cannot save object references. However, you can save other kinds of variables. You can use these variables to talk about your obj reference. For example if I have a sword…when I interact with that sword in some way and I want to save it (example attaching it to the player), then instead of trying to save its reference I can create a boolean that just says “IsSwordAttached” and save that boolean instead. Then when I load game if that boolean is true then just re-attach the weapon. If you have multiple weapons, then you just need to create an Enumerator that will handle them all. And instead of saving a boolean, you can just create variables with its type set as your enumerator, and save the variables. Then when you load, run a switch for each enum value and do the same as before.

but if the sword need information too, like how damaged it is lets say i damaged it and sword durability is now 65%

how would i save it then ? because i cant make 101 enumerations for each sword durability from 0 to 100 and what if i had other variables for the sword to save too

there should be an other and easy way to save object reference

Save the sword item as a struct containing the enum type of your sword and a float for your durability
Saves are meant to be concise and loading everything that was already in place could lead to more bugs, so restarting fresh everytime you load the game at the cost of some code is a fair trade, and it even save storage space.