I keep getting Invalid get index '18' (on base: 'Nil') godot4 broke my save

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mangojuice

the savegame.dat file exists but it cant be read i guess i also get “something went wrong loading” error

@onready var path = "user://savegame.dat"

func save_data():
	var file = FileAccess.open(path,FileAccess.WRITE)
	file.store_var(level_info)
	if not file.fileAccess_exists(path):
		print("something_went_wrong_saving")
		return
	file.close()
	file.store_text()

func load_data():
	if not FileAccess.file_exists(path):
		print("something_went_wrong_loading")
		return
	var file = FileAccess.open(path,FileAccess.READ)
	var data=file.get_var()
	return data

Edited to fix code formatting. In the future when adding code to a forum post, select the pasted code and press the {} button to format it for the forum.

jgodfrey | 2023-03-05 16:10

Why are you certain the file exists? Can you see it by some other means? Can you open it by some other means? Do you possibly have a permissions issue?

jgodfrey | 2023-03-05 16:11

Additionally, this code in your save_data() method looks problematic:

file.store_text()

For starters, FileAccess doesn’t have a store_text() function. Further, even if it did, you’ve closed the file on the line immediately prior, so you can’t store anything in it anyway.

jgodfrey | 2023-03-05 18:13

func _ready():
	level_info = load_data()
	which_world()

var current_world=1
func which_world():
	if GameDataManager.level_info[18]["unlocked"] == true:
		current_world=2

@onready var path = "user://savegame.dat"

func save_data():
	var file = FileAccess.open(path,FileAccess.WRITE)
	file.store_var(level_info)
	if not file.fileAccess_exists(path):
		print("something_went_wrong_saving")
		return
	file.close()

func load_data():
	if not FileAccess.file_exists(path):
		print("something_went_wrong_loading")
		return
	var file = FileAccess.open(path,FileAccess.READ)
	var data=file.get_var()
	return data

mangojuice | 2023-03-06 09:04

you are right savegame.dat doesnt exist i removed the old ones created by 3.4 when i run the godot 4 project it doesnt get created

mangojuice | 2023-03-06 09:07

i gave godot full drive acces so i dont think its a permission issue

mangojuice | 2023-03-06 09:08

any ideas how i can print out the return code from save function

mangojuice | 2023-03-06 09:13

here is how i fixed it thank you for the feedback

func _ready():
    	save_data(level_info)

onready var path = "user://savegame.dat"

func save_data(data):
	var file = FileAccess.open(path,FileAccess.WRITE)
	file.store_var(data)
	if not FileAccess.file_exists(path):
		print("something_went_wrong_saving")
		return
	file.close()

func load_data():
	var file = FileAccess.open(path,FileAccess.READ)
	if not FileAccess.file_exists(path):
		print("something_went_wrong_loading")
		return
	var content=file.get_var()
	return content

mangojuice | 2023-03-06 11:31

:bust_in_silhouette: Reply From: mangojuice
func _ready():
        save_data(level_info)

onready var path = "user://savegame.dat"

func save_data(data):
    var file = FileAccess.open(path,FileAccess.WRITE)
    file.store_var(data)
    if not FileAccess.file_exists(path):
        print("something_went_wrong_saving")
        return
    file.close()

func load_data():
    var file = FileAccess.open(path,FileAccess.READ)
    if not FileAccess.file_exists(path):
        print("something_went_wrong_loading")
        return
    var content=file.get_var()
    return content