+1 vote

I'm following this tutorial for learning how to load and save games, but I'm come across a problem when I try to test it.

I get the error:

invalid get index 'save_path' (on base: 'null instance').

even though "save_path" is defined.

Here's my code:

    extends Node

export(StringArray) var save_data_names
export(Array) var saveData
var save_data = {}
var savegame = File.new()
export var save_path = "user://user.bin"
onready var global_node = get_node("/root/global")

func _ready():
    # Called every time the node is added to the scene.
    # Initialization here
    if save_data_names:
        for i in range(save_data_names.size()):
            save_data[save_data_names[i]] = saveData[i]
    check_savegame()

func check_savegame():
    print(savegame.file_exists(save_path))
    if !savegame.file_exists(save_path):
        savegame.open_encrypted_with_pass(save_path, File.WRITE, OS.get_unique_ID())
        savegame.store_var(save_data)
        savegame.close()
    else:
        savegame.open_encrypted_with_pass(global_node.save_path, File.READ, OS.get_unique_ID())
        save_data = savegame.get_var()
        savegame.close()

func saveData():
    savegame.open_encrypted_with_pass(save_path, File.WRITE, OS.get_unique_ID())
    savegame.store_var(save_data)
    savegame.close()

func setKey(key, value):
    save_data[key] = value

func getValue(key):
    if saveData.has(key):
        return saveData[key]
    else:
        return null

What do I do?

in Engine by (171 points)

Are you trying to access save_path from somewhere else?
I think it says you are trying to get save_path from a null reference.

savegame.open_encrypted_with_pass(global_node.save_path, File.READ, OS.get_unique_ID())
are you sure global_node has save_path variable?

1 Answer

+1 vote
Best answer

I found the answer. I removed the "global_node" value and changed:

savegame.open_encrypted_with_pass(global_node.save_path, File.READ, OS.get_unique_ID())

to

savegame.open_encrypted_with_pass(save_path, File.READ, OS.get_unique_ID())

and that fixed the problem!

by (171 points)
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.