Godot Version
4.5.1.stable
Question
I have a script named “SaveLoad”. I made it an Autoload, so I can access the save/load mechanic, no matter the scene it comes from. However, when I tried the code below, I get an “Invalid access to properties or key” error on the “contents_to_save[keys] = data.contents_to_save[keys]” line.
extends Node
const save_location = "user://SaveFile.json"
var contents_to_save: Dictionary = {
"Cities": WorldMap.cities,
"Active_cities": WorldMap.active_cities,
"Weekly_expenses": WorldMap.weekly_expenses,
"Location_purchase": WorldMap.location_purchase,
"Airship_purchase": WorldMap.airship_purchase
}
# Called when the node enters the scene tree for the first time.
func _save():
var file = FileAccess.open(save_location, FileAccess.WRITE)
file.store_var(contents_to_save.duplicate())
file.close()
func _load():
if FileAccess.file_exists(save_location):
var file = FileAccess.open(save_location, FileAccess.READ)
var data = file.get_var()
file.close()
for keys in contents_to_save.keys():
contents_to_save[keys] = data.contents_to_save[keys]