0 votes

how to save this dictionary:

var save_vars = {
    "highscore":highscore,
    "games_played":games_played
}

into a file?

I couldn't figure it out from docs or other posts...

Godot version 3.3
in Engine by (25 points)

1 Answer

0 votes
Best answer

Something like this should work:

onready var persistFile = "user://savedata.txt"
onready var saveHandle = File.new()

func saveUserData(dict):
    saveHandle.open(persistFile, File.WRITE)
    saveHandle.store_line(to_json(dict))
    saveHandle.close()

Then, call saveUserData() like:

saveUserData(save_vars)

Note, the above is extracted from a working persistence script, so you may need to season it to taste.

by (19,270 points)
selected by

and how do i convert the json to dict? asking because i need to load it

edit: nvm i got it thanks for helping :)

I see you got it, but in case it's helpful, to load, use something like this:

saveHandle.open(persistFile, File.READ)
saveDict = parse_json(saveHandle.get_line())
saveHandle.close()
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 Frequently asked questions and 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 [email protected] with your username.