Hi........................ ## (Godot v3.1.1) ##
i use this script for saving and loading game..
extends Node
const SAVE_PATH = "res://save.json"
func _ready():
load_game()
func save_game():
var save_file = File.new()
save_file.open(SAVE_PATH, File.WRITE)
var save_dict = {}
var nodes_to_save = get_tree().get_nodes_in_group("Settings")
for node in nodes_to_save:
save_dict[node.get_path()] = node.save()
save_file.store_line(to_json(save_dict))
save_file.close()
func load_game():
var save_file = File.new()
if not save_file.file_exists(SAVE_PATH):
print("The save file does not exist.")
return
save_file.open(SAVE_PATH, File.READ)
var data = parse_json(save_file.get_as_text())
for node_path in data.keys():
var node_data = data[node_path]
get_node(node_path).load_state(node_data)
now, i have two scene: scene1 & scene2... both of them are in "Settings" group..
loading and saving in scene1 works fine.. i add these line to achive this..
func _ready(): #i want to save a boolean value
SavedGame.load_game()
.
.
.
func _on_Timer5_timeout(): #this function triggers with a timer
SavedGame.save_game()
.
.
func save():
var save_dict = {
first_time = false
}
return save_dict
func load_state(data):
for attribute in data:
if attribute == 'first_time':
first_run = false
else:
set(attribute, data[attribute])
but loading and saving in scene2 does not work and godot shows this error:
"Attempt to call function 'load_state' in base 'null instance' on a null instance."
var player_name #i want to save player's name in scene2
func _ready():
SavedGame.load_game()
.
.
func _on_OK_button_up():
SavedGame.save_game()
.
.
func save():
var save_dict = {
player_name = player_name
}
return save_dict
func load_state(data):
for attribute in data:
if attribute == 'player_name':
player_name = attribute
else:
set(attribute, data[attribute])
what am i doing wrong in scene2? thank you for your time :)