Topic was automatically imported from the old Question2Answer platform.
Asked By
Yasi
I want users to be able to save and load their data (json) by entering the data inside the game.
Here’s what I have so far:
var dictionary = {}
var loaddictionary
func _on_CopySaveCode_pressed():
for i in range(1, 83):
dictionary[str(i)] = {
"L1": DataImport.player_data_save[str(i)].L1,
"CLVL": DataImport.player_data_save[str(i)].CLVL,
}
OS.set_clipboard(str(dictionary))
func _on_PasteLoadCode_text_entered(new_text):
loaddictionary = new_text
var d = Directory.new()
if not d.dir_exists("user://test"):
d.make_dir("user://test")
var file = File.new()
file.open("user://test/PlayerData.json", File.WRITE)
file.store_string(to_json(loaddictionary))
file.close()
The first problem is that when it’s copied to clipboard, it doesn’t copy each dictionary[str(i)] as a key. For example, it just adds it as 1 instead of “1”
The second problem is that the file is written as a string instead of a dictionary. So the whole dictionary is wrapped around “” when it should be {}
To get the double quotes to show up when pasted from the clipboard, you can encase them with single quotes and use string formatting to put your key inside like so: '"%s"' % str(i)
func _on_CopySaveCode_pressed():
for i in range(1, 83):
dictionary['"%s"' % str(i)] = {
If want the values to also show up with quotes, you can do the same here
To save the file as json use file.store_line() instead of file.store_string()
func _on_PasteLoadCode_text_entered(new_text):
loaddictionary = new_text
var d = Directory.new()
if not d.dir_exists("user://test"):
d.make_dir("user://test")
var file = File.new()
file.open("user://test/PlayerData.json", File.WRITE)
file.store_line(to_json(loaddictionary))
file.close()
Thanks for the help. The first part works and everything looks as intended when I paste in notepad. However, the file output is still not right. It’s outputting the following:
"{\"1\":{\"CLVL\":1, \"L1\":1}..."
It still has the quotation marks at the beginning and at the end. And now it has the \ for some reason. I’m using LineEdit for the input.