Is it safe to save array elements?

I am saving my hero’s ammunition counts for each weapon with different variables. When I hit the save, an array like “int saveables” collecting infos from other variables and push it to the Saving System. When I load the game, saveables returns the values and deliver them to the proper weapons.

I am also using this for position too, with float array. It’s been a while and I have no problem with that but, is that correct usage? I feel a little bit of suspicious with that. Btw I am using JSonSaveable System.

My personal preference for something like an ammo count, since each ammo is associated with a different weapon, is to use a JObject, which is a Dictionary<string, JToken>
Then I can apply a specific “key” to each ammo, perhaps the weapon’s name or if you’re using our Inventory system, the weapon’s itemID.

That being said, if you’re always using the same array index for the same weapon, then you could use a JArray.

I actually recommend against simply passing an float[] to the JToken, as I prefer to build my data structures directly in Capture/Restore and leave out the possibility that it’s misinterpreted. Doesn’t mean you can’t, that’s just a personal preference of mine. With the newer versions of NewtonSoft (such as what is installed by default with Unity 2021 or later), you no longer need to overlay JObjects and JTokens with IDictionary<string,JToken> or IList<JToken>, you can access the values directly and treat a JObject exactly like a Dictionary<string, JToken> and a JArray exactly like a List<JToken>

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms