What is the best way to convert a List of reference types to a string array?

I’m using a third party asset called Easy Save 3. You can see my dilemma here:
https://moodkie.com/forum/viewtopic.php?f=13&t=2155&p=8136#p8136

If you have any desire to read the documentation, it’s here:

So basically using this tool I can add the game objects my load/save is referencing by adding this a “Save Manager” to the scene, but I would rather just save an array of strings and load it back into the scene by updating the list.

In other words I want to:

  • Save List to String[ ] of Ranged.weaponName
  • Load Array back into List

So I just need a little help converting referenced objects to and from strings I’m pulling from those objects. The logic is new to me, and not sure where to start or what to Google.

Any help is appreciated, thank you!

I would look into using json to convert the list into a json string, then save that out and then load and convert it back when you want to retrieve the saved data.

The general term for converting data into strings and back again is “serialization” (and “deserialization”). JSON is a common style of string to convert things into.

2 Likes

Did a little research and this seems like a decent option. But is it even possible to save an entire list of gameobjects as a json string? Struggling to find a tut on that.

So after some further research I found this awesome video:

However, as it turns out, this method is pretty much what the Easy Save 3 tool is doing with the Save Manager. So I guess I’ll just save myself some time and use what they’re providing.

Thank you for the help!

If saving a single X would be easy, then saving “an entire list of X” is usually also easy.

But saving “a GameObject” is probably not something you can easily do, and probably not something you ought to do.

Saving a complex code object usually isn’t as simple as “take a snapshot of whatever is currently in the running program”. It may have some incidental or cached data that it would be wasteful to save, because it can be easily recomputed from other data–for instance, there’s no need to save an object’s rotation as both a Quaternion and Euler angles. More importantly, it may have relational data that only makes sense in the context of the running program–usually a reference to some external object or concept, such as the active menu or the current time.

If your class is just “dumb data”–basically just a collection of primitive data types like floats and strings, or other “nested” dumb data classes–you can probably serialize it and deserialize it just by calling some convenient library function.

But if your class is more than just dumb data, you probably need to make decisions about which parts are essential and how to represent them. For example, instead of trying to save a “live” reference to a particular player skill, you save the skill ID instead, which is enough information to find the appropriate skill and re-create the reference after you load. And maybe you don’t bother saving a reference to the player at all, because there’s always one player and they’re easy to find and the only reason you were saving it in the first place was for efficiency. And then you need to write some custom code for your particular class that pulls out just the essential data when saving, and then recalculates everything else from just the essential data when loading.

1 Like