|
|
|
 |
Reply From: |
stormreaver |
Since this is an old thread, I’m sure you’ve already discovered this; but for those struggling, the answer is to use a dictionary (the original poster was using an array) in the object to store your data. Dictionaries are directly useable by to_json() and parse_json(), while arrays are not.
Do not store your data in individual variables, as you will then have to construct/deconstruct a dictionary manually for the save and load operations. To insulate your program from your internal data representation, use accessor methods that read/write your dictionary rather than directly referencing your dictionary components.
Using this approach, you can add and remove variable items without ever having to modify your save and load functions.
Hi. Thanks for your comment. I tried that approach and that didn’t work for me. It’s possible that my code was faulty though. Is there some sample code you could share for the initialization, the saving, and the loading?
lordbry | 2021-06-28 15:44
Here’s my core code for my Player class:
extends Node
var m_aData = {}
func getPassword():
return m_aData["password"]
func setPassword(a):
m_aData["password"] = a
func getData():
return m_aData
func setData(a):
m_aData = a
Add additional accessor methods to add support for additional fields. These will get and set the m_aData dictionary. The initial dictionary is empty, and automatically creates new entries when a set*() method references one that doesn’t already exist. This is the only file you will ever have to update when you add or remove fields from the Player class.
Reading and writing the JSON files will automatically read/write the new fields you add to Player, and you can follow this pattern for every persistent data type you want to add to your game.
Here’s how I save the dictionay to a JSON file:
func createAccount(stAccountname,stUsername,stPassword,stPath):
print("Preparing new account")
var player = load("res://player/Player.gd").new()
var stData
player.setAccountName(stAccountname)
player.setPlayerName(stUsername)
player.setPassword(stPassword)
stData = player.getData()
return ConfigurationWriter.write(stPath,to_json(stData))
ConfigurationWriter.write() is just a helper function that opens a file, writes the JSON text, and closes the file; with error handling built-in. You will never have to modify this function to add or remove Player data fields.
Here’s an example of reading the player data to validate the password:
func authenticatePassword(stPath,stPassword):
var ret = false
print("Authenticating password")
if stPath != null && stPassword != null:
var stData = ConfigurationReader.read(stPath)
var aData
if stData != null:
aData = parse_json(stData)
ret = aData["password"] == stPassword
else:
print("Can't read data from " + stPath)
return ret;
Again, ConfigurationReader.read() is just a convenience method to open a file, read it, and return the raw text data (saved in JSON format).
stormreaver | 2021-06-28 16:22