SaveData: Struct Merge

I am fiddling with save game data.
I developed some working logic, which is great.
I’ve since refactored and updated my logic to instead of determine everything to save at once, to save data into indexes upon changes, removing outdated indexes as it saves new indexes.

The problem arises because in my save game I’m using an array of a structure to store multiple instances of multiple types. I want to only update the changes to the save data BY PASSING in the updates in a struct of the same type.
In the end I’d like to merge the structs and leave out the null/blank/empty data.

One problem is the compiler identifies an issue with passing a struct without an array of at least one element.
To do the effect you’d have to write each variable to identify the change separately at the moment it seems.

I believe my question is similar to the issue this user was having in 2015 (Structure Merge - Audio - Unreal Engine Forums)

Perhaps I’ll reconstruct my logic to make specific updates to the save data instead of passing chunks in the form of a struct when I want an update to pull the updates. In the interim I think it would be nice to merge struct with another struct and for changed values to overwrite the other values.

If there is a great way or a method to merge a struct with another structs (non null) values. I’d like to be informed.

Thanks,
SmashRash

So in response to my own question I remembered more accurately why this was an issue.

In order to capture that the variable changed, I would have to change the save data IMMEDIATELY when the value changes, which would mean my save game data would immediately update (although not be saved). However if I wanted to change it later, I need to know that the value was (at one point) updated and therefore should be overwritten. To determine if it was at one point updated I have to do a variety of checks which would be resolved by simply merging overwrites between a struct with assigned variables and null values.

I’m going to attempt to use Maps and Map functionality with “insert”,overwriting elements, and “swap”. As discussed in this stackoverflow post: c++ - How can I merge two STL maps? - Stack Overflow

If you want to copy entries from one
map to another, you can use std::map’s
insert:

targetMap.insert(sourceMap.begin(),
sourceMap.end()); But note that insert
does not update elements if their key
is already in targetMap; those items
will be left as-is. To overwrite
elements, you will have to copy
explicitly, e.g.:

for(auto& it : sourceMap) {
targetMap[it.first] = it.second; } If you don’t mind losing the data in
sourceMap, another way to achieve a
copy-and-overwrite is to insert the
target into the source and std::swap
the results:

sourceMap.insert(targetMap.begin(),
targetMap.end()); std::swap(sourceMap,
targetMap); After swapping, sourceMap
will contain targetMap’s old data, and
targetMap will be a merge of the two
maps, with preference for sourceMap’s
entries.

So two solutions presented themselves though not as directly as the link I supplied in this question.

Either I pull in the struct upon load and save a duplicate copy that I update and then pass back to overwrite the original, when I want the information to be written to the active data (and then followed by saved).

Or

I build a mapping using key-value pairs (of a string key - to a struct value). I’m leaning toward the mappings because it will allow me to save values in multiple ways, overwrite only changes, allow for delays as well as have the possibility for multiple revisions by simply changing the key name. Since it leaves the possibility for excess data stored I’ll likely perform a cleanup on the keys at some point, however it does not resolve storage as every key associated with a struct (value) will have only one value edited in the struct and null data in the remaining.

Key / StructValue

v0.0_Name-0 / Name (set to value)
/ Number (null)
/Description (null)

v0.0_Number-0 / Name (null)
/ Number (set to a value)
/Description (null)

v1.0_Number-2 / Name (null)
/ Number (set to a value)
/Description (null)

Hence extra null values
As well as excess v0.0_Number-0 and v1.0_Number-2 values which I like having the possibility of.

Marking this as resolved,
SmashRash

So two solutions presented themselves though not as directly as the link I supplied in this question.

Either I pull in the struct upon load and save a duplicate copy that I update and then pass back to overwrite the original, when I want the information to be written to the active data (and then followed by saved).

Or

I build a mapping using key-value pairs (of a string key - to a struct value). I’m leaning toward the mappings because it will allow me to save values in multiple ways, overwrite only changes, allow for delays as well as have the possibility for multiple revisions by simply changing the key name. Since it leaves the possibility for excess data stored I’ll likely perform a cleanup on the keys at some point, however it does not resolve storage as every key associated with a struct (value) will have only one value edited in the struct and null data in the remaining.

Key / StructValue

v0.0_Name-0 / Name (set to value)
/ Number (null)
/Description (null)

v0.0_Number-0 / Name (null)
/ Number (set to a value)
/Description (null)

v1.0_Number-2 / Name (null)
/ Number (set to a value)
/Description (null)

Hence extra null values
As well as excess v0.0_Number-0 and v1.0_Number-2 values which I like having the possibility of.

Marking this as resolved,
SmashRash