Help - How to fix an (on base: 'Dictionary') error

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

This is my first game . I am making a save system and getting an error as follows:
Invalid get index ‘unlockedlevel’ (on base: ‘Dictionary’) . Searched for answers online and did not got any . Please help me


var coins = 0
var levels = []
var unlockedlevel = 1


#save game
func save_game():
	var save_file ="user://" , File.WRITE)
	var data = {

#load game
func load_game():
	var save_file =
	if !save_file.file_exists("user://"):
		return"user://" , File.READ)
	var data = save_file.get_var()
	coins = data["coins"]
	unlockedlevel = data["unlockedlevel"] #error

If the words need to be scrunched together, I would suggest using “camel case” for key names, e.g. UnlockedLevel. That makes it a bit easier to read the name. I would also suggest checking for the key name in the dictionary:

if data.has("UnlockedLevel"):
    unlockedlevel = data["UnlockedLevel"]

That way, the game can be programmed to react to data that isn’t there. If the data isn’t being saved to a file, or the file doesn’t have the data, then that issue will have to be addressed.

Ertain | 2021-10-16 09:30