How do I load a saved array?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ismaelgame7
:warning: Old Version Published before Godot 3 was released.

Hello people,

I would like to know how do I load a saved array?
Example:

var achievements = [0,0,1,0,1]

I tried using get_var like this: achievements = get_var ()
but what did not work. :
the array gets null, always when I try to load the game
Can someone help me solve this little problem? :slight_smile:

:bust_in_silhouette: Reply From: ismaelgame7

You can not help me?
I’m trying to make a system of achievements using a simple array.
The problem is that I’m getting this error:

I was wondering if I’m doing something wrong at the moment of saving or loading the game.
look:

please anyone know how to solve this?

Saving an array should work the way you’re doing it. Have you checked that the other variables are being loaded properly? The saveGame() function is missing a save.close() command although I’m not sure if it makes any difference.

mollusca | 2018-01-11 09:48

Hello, maybe this is it, I forgot to add save.close ()
Well, there is no way to check, since I have reformulated the functions using JSON.
Thank you anyway!!!

ismaelgame7 | 2018-01-11 11:11

:bust_in_silhouette: Reply From: ismaelgame7

I hope this helps someone who is having problems / difficulty doing this:

var file = File.new()
var savePath = "user://Save.json"
var safeScene = "Level1-1"
var score = 0
var achievements = [0,0,1,0,1]
var canMusic = true

#Save & Load
func saveGame():
	var dict = {
		"safeScene":safeScene,
		"score":score,
		"achievements":achievements,
		"canMusic":canMusic,
	}
	
	file.open(savePath,file.WRITE)
	file.store_line(dict.to_json())
	file.close()


func loadGame():
	if file.file_exists(savePath):
		file.open(savePath,file.READ)
		var tmp_data = file.get_as_text()
		file.close()
		
		var dict = {}
		dict.parse_json(tmp_data)
		
		safeScene = dict["safeScene"]
		score = dict["score"]
		achievements= dict["achievements"]
		canMusic = dict["canMusic"]

thank you, brother, it helpme so much but i think some of the code is not wor anymore, like on
dict.to_json() now it change to to_json(dict)
dict.parse_json(tmp_data) now it change to dict=parse_json(tmp_data)
cmiiw, but thank yo so much

wahyuadiramadan | 2019-12-27 17:03