0 votes

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 {}

Godot version 3.3
in Engine by (18 points)

1 Answer

0 votes

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

            "L1": '"%s"' % str(DataImport.player_data_save[str(i)].L1),
            "CLVL": '"%s"' % str(DataImport.player_data_save[str(i)].CLVL),
            }
    OS.set_clipboard(str(dictionary))

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()
by (22 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.