Writing optional data?

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
Locked
viveleroi
Posts: 17
Joined: Fri Feb 03, 2017 1:40 am

Writing optional data?

Post by viveleroi »

I've created a custom type and need to tweak the save logic, but the data I'm saving may be null. Since the order of read has to match the order of write, how can I properly include optional data? Does there have to be a null value? Does seem like ES2 handles null so I'm wondering what my options are.

Edit: Looking through the API, I think I can do what I need by using the tag form of reading/writing. That it's not sequential and I can make use of TagExists.

Edit2: I've given it a try but I must be doing something wrong. This code: https://pste.me/#/2o0fi/5fxXMoy01TtwmR8 ... kFg7uMFNYE writes just fine, the files are written, they seem to be about 600k depending on the data but when I try to read from them, I get the error:

Code: Select all

ES2InvalidDataException: Easy Save 2 Error: The file provided does not contain data that is readable by Easy Save. Please make sure that file was created by Easy Save.
ES2Reader.Next ()
ES2Reader.ScanToTag (System.String tag)
ES2Reader.ProcessHeader (Key expectedCollectionType, .ES2Type expectedValue, .ES2Type expectedKey, System.String tag)
ES2Reader.Read[Chunk] (System.String tag)
ES2.Load[Chunk] (System.String identifier)
SaveManager.LoadChunk (Vector2 chunkVec) (at Assets/Scripts/Saves/SaveManager.cs:68)
World.loadChunk (Vector2 chunkVec) (at Assets/Scripts/World/World.cs:154)
World.loadChunksAround (Vector2 origin, Int32 radiusX, Int32 radiusY) (at Assets/Scripts/World/World.cs:198)
TilingEngine.Update () (at Assets/Scripts/Rendering/TilingEngine.cs:41)
I think in the long run I would prefer to go the sequential route because it's said to offer better performance, and that will matter here because this will involve loading terrain.

Unless there's something better that I'm missing, I've solved this for now by using a boolean which is always written to the file, and tells me whether the next piece of optional data exists.

Code: Select all

var hasEntity = reader.Read<bool>();

if (hasEntity) {
   // read it here
}
User avatar
Joel
Moodkie Staff
Posts: 4863
Joined: Wed Nov 07, 2012 10:32 pm

Re: Writing optional data?

Post by Joel »

Hi there,

Writing a boolean before the piece of data to tell whether it exists is the correct way of doing what you wish to achieve (as you've discovered).

With regards to the error you were having with your code, you cannot use TagExists inside an ES2Type. ES2Types should only contain sequential writes, but TagExists is for random-access at the top level of the file.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Locked