I'm trying to implement saving and loading the game state in an adventure game. I tried the approach from Saving games, but I realized there are too many variables to save: the position and rotation of the player, the position of every animated thing in the game (e.g. slowly closing door, rotating fan), the fact if each interactive item was activated already (e.g. pickable items cannot reappear if they were already picked up) and so on. I've found this comment on GitHub and it seems much more promising to just save the entire current scene of the game and then load it.
I managed to save the scene using something like this:
var current_scene = get_tree().get_current_scene()
var scene = PackedScene.new()
var e = scene.pack(current_scene)
e = ResourceSaver.save("user://test.tscn", scene)
func _set_owner(node, root):
if node != root:
node.owner = root
for child in node.get_children():
I've tested it on simple scenes and it seems to save the entire state (e.g. if a checkbox is checked), so this looks like a solution I'm looking for. I've saved the entire gameplay scene that way and the program didn't crash. But there is a problem when I try to load it.
The code I've used looks more or less like this:
The results look like this:
First problem: the colors and lighting look horrible, the mouse doesn't work. and many interface elements are doubled. I'm pretty sure that the original gameplay scene was removed by
change_scene, so there shouldn't be 2 scenes.
Second problem: the
_ready function gets called. Many elements of the game are initialized using the
_ready function, and it should not be called again when loading a saved scene. That will cause e.g. the initial animation to be played again, the picked up items to reappear etc.
What I'd like to achieve is to load the scene in the exact state as it was saved and attach it to the game. It can be done by replacing the current scene, but it can be also just a single node in the current scene (e.g. I can assume there is always a node called "Gameplay" and only this one will be saved and loaded). I think I've tried all combinations of
ResourceLoader and a few other things, but I couldn't find a proper solution. Can it be done in Godot Engine?