Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Hailrig |
I followed the tutorial for saving in Godot 3, and implemented it in my game with only minor changes (the official Godot saving docs tutorial had some errors) but otherwise I implemented it with no changes. I set a door to be a persistent object, and I save a handful of variables including the rotation of the door. However, when I load the door I am unable to refer to the door via get_node or via signals. The door otherwise behaves as expected, and my signals work if I do not load the game.
Interestingly, when I save the game with the door and then delete the door before running the game, it works perfectly - loading only one copy of the door, from the save. To check my theory, I print(name) in the ready of the door and it prints two names - Door, and @Door@6 - I assume this is the name it is was given after loading, but I am not sure why.
I have attached the appropriate code, hopefully someone can indicate what the problem is and how I should resolve it.
Thanks a pile.
Loading code:
func load_game():
var save_game = File.new()
if not save_game.file_exists("res://savegame.save"):
return # Error! We don't have a save to load.
# We need to revert the game state so we're not cloning objects during loading. This will vary wildly depending on the needs of a project, so take care with this step.
# For our example, we will accomplish this by deleting savable objects.
var save_nodes = get_tree().get_nodes_in_group("Persist")
for i in save_nodes:
i.queue_free()
# Load the file line by line and process that dictionary to restore the object it represents
save_game.open("res://savegame.save", File.READ)
var current_line = parse_json(save_game.get_as_text())
# First we need to create the object and add it to the tree and set its position.
var new_object = load(current_line["filename"]).instance()
get_node(current_line["parent"]).add_child(new_object)
new_object.position = Vector2(current_line["pos_x"], current_line["pos_y"])
# Now we set the remaining variables.
for i in current_line.keys():
if i == "filename" or i == "parent" or i == "pos_x" or i == "pos_y":
continue
new_object.set(i, current_line[i])
save_game.close()
Saving Code:
func save_game():
var save_game = File.new()
save_game.open("res://savegame.save", File.WRITE)
var save_nodes = get_tree().get_nodes_in_group("Persist")
for i in save_nodes:
var node_data = i.save();
save_game.store_line(to_json(node_data))
save_game.close()
Save file contents: {“door_stop”:false,“filename”:“res://Maps/Door.tscn”,“name”:“Door”,“parent”:“/root/TestMap”,“pos_x”:24.874701,“pos_y”:233.261993,“rotation”:0}