Save variable to JSON file

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By chaosreborn1

So I have a JSON file with multiple entries and variables like this

{
“1”: {
“Name”: “Player1”,
“Race”: “Elf”,
“SubRace”: “Wood Elf”,
“Class”: “Wizard”,
“Str”: 10,
“Dex”: 10,
“Con”: 10,
“Int”: 10,
“Wis”: 10,
“Cha”: 10
},
“2”: {
“Name”: “Player2”,
“Race”: “Dwarf”,
“SubRace”: “Hill Dwarf”,
“Class”: “Fighter”,
“Str”: 10,
“Dex”: 10,
“Con”: 10,
“Int”: 10,
“Wis”: 10,
“Cha”: 10
}
}

Say if I want to change one variable from one entry, for example the “Race” variable of entry 2 from the JSON file and save it accordingly, so that I can assign that function to a button. How do I write it in GDScript?

:bust_in_silhouette: Reply From: bchmura

JSON is typically not used for storing the actual application state in… but more for storing and retrieving it from a file or service.

What you would do is load it when the program starts into something your application would use while it runs, and then on exit you would save it to the JSON. While running you would just modify your in memory data structure. partly because I/O operations are slow and can impact your game flow.

I’ve not tested the contents of this video out, but looks like it is what you are trying to do.

To line it up with my above comment he talks about loading and saving from Godot’s dictionary type.