Having trouble storing an array to a save file

Godot Version

4.2

Question

I am making a game where I need to store the position y of every player death I have this piece of code to write the array to the file:

func SAVE():
	FileAccess.open("user://save",FileAccess.WRITE).store_var(array)

My array is:

var array = ["Test", "Testing", 69420]

And my load code is:

func OPEN_READ():
	read = FileAccess.open("user://save",FileAccess.READ)
func LOAD():
	for arraything in read.get_var():
		print(arraything)

And it errors out, giving me a very non-descriptive error, “TEST.tscn::GDScript_ehyxi:15 @ LOAD(): Condition “len < 4” is true. Returning: ERR_INVALID_DATA”

Which I assumed it was because I had less than 4 items in the array, but it doesn’t matter if I add another one, 10 or 100, it still gives that error.

UPDATE: I changed

func SAVE():
	FileAccess.open("user://save",FileAccess.WRITE).store_var(array)

Into

func SAVE():
	var save = FileAccess.open("user://save",FileAccess.WRITE)
	save.store_var(array)
	save.close()

And the load part into:

func LOAD():
	for arraything in FileAccess.open("user://save",FileAccess.READ).get_var():
		print(arraything)

And it works now!!

1 Like