0
\$\begingroup\$

I am working in Unity, on my first project. I am working on a turn-based RPG, and I am having a hard time figuring out my back end.

I'm currently using JSON files to store my data, which are then converted into objects and processed in C#. So far so good, but I have reached a road block when trying to save data back into a JSON file.

For example, lets say I have a character file such as: [{"name":"John", "CurrentHP": 100, "MaxHP": 100},{"name":"Paul", "CurrentHP": 200, "MaxHP": 200}]. After a combat, John loses 30 hp. How do I save this data back to that JSON file, so it reads as [{"name":"John", "CurrentHP": 70, "MaxHP": 100},{"name":"Paul", "CurrentHP": 200, "MaxHP": 200}]?

The tutorials I've watched all talk about serializing the object back into a JSON file, but this would involve deleting the whole file and creating a new one with the new information. This sounds like a very ineffective approach, especially for some large 'tables' in my game. There needs to be a way to update just one attribute from one of the IDs in a given JSON file.

How do I efficiently update individual JSON rows?

\$\endgroup\$
2
\$\begingroup\$

JSON is a string-based format. When you change the length of something in a JSON file (for example by changing "CurrentHP": 99 to "CurrentHP": 101 everything which comes after that in the JSON file needs to be rewritten because it's now at the wrong offset. That's how string-based file formats work. For that reason JSON libraries rarely support in-place replacement of individual fields, and Unity is no exception.

To mitigate the problem you might want to avoid too large JSON files. Split information into multiple files instead. For example, when you have a large table of objects, make each row an individual file. There is no law which says each savegame must be a single file and can not be a directory of multiple files.

Alternatively, you could use a proper database. Like SQLite, for example.

You should also avoid unnecessary saving. You generally don't need to save the complete gamestate whenever anything in the game changes. It is completely adequate to save when the player tells you to and/or autosave at regular checkpoints.

\$\endgroup\$
0
\$\begingroup\$

Make two files, GameData.json for storing actual data and GameState.json for holding the the current state. When your character initialized first time, assign their stats from GameData. When saving state of your character, write on GameState. SO next time when your character load, get state from GameState.

\$\endgroup\$
  • \$\begingroup\$ Don't use JSON for runtime operations. JSON is for serialization only. \$\endgroup\$ – Draco18s May 5 '17 at 13:22
  • \$\begingroup\$ @Draco18s How can you say that? You can always save data to JSON. All big companies do that for online games. Their data is saved with some database like mongodb in the form of JSON. I have been using GameSparks and they use MongoDB to store/modify data in the form of JSON. \$\endgroup\$ – Saad Anees May 6 '17 at 8:18
  • \$\begingroup\$ ...That's serialization. Anything that "saves" or "sends" data is serialization. Think of JSON as a zip file: you can easy convert between the compressed data and the uncompressed data, but modifying the compressed data is slow and often involves uncompressing the data, modifying it, and recompressing it again. \$\endgroup\$ – Draco18s May 6 '17 at 15:47
  • \$\begingroup\$ @Draco18s So please tell me what should we do to save and modify our data? \$\endgroup\$ – Saad Anees May 9 '17 at 19:50
  • \$\begingroup\$ "Save" and "Modify" are not even close to the same operation. "Save" means you're sticking it in an envelope, licking the seal, filing it away in a safety deposit box and putting the key on your key ring. You can't modify saved data: you can only read it into memory from disk and write it to disk from memory. If you want to modify it, you must modify it in memory. If it exists in memory it exists as an Object which has been deserialized. \$\endgroup\$ – Draco18s May 9 '17 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.