Attention | Topic was automatically imported from the old Question2Answer platform. | |
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
#variables
var coins = 0
var levels = []
var unlockedlevel = 1
#functions
#save game
func save_game():
var save_file = File.new()
save_file.open("user://save_game.save" , File.WRITE)
var data = {
"coins":coins,
"unlockedlevel":unlockedlevel
}
save_file.store_var(data)
save_file.close()
#load game
func load_game():
var save_file = File.new()
if !save_file.file_exists("user://save_game.save"):
return
save_file.open("user://save_game.save" , File.READ)
var data = save_file.get_var()
coins = data["coins"]
unlockedlevel = data["unlockedlevel"] #error
save_file.close()
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