0 votes

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()
Godot version 3.3.4
in Engine by (12 points)
edited by

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.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.