![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Miskatonic Studio |
Hi!
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:
func _input(event):
if Input.is_action_just_pressed("goat_save"):
var current_scene = get_tree().get_current_scene()
_set_owner(current_scene, current_scene)
var scene = PackedScene.new()
var e = scene.pack(current_scene)
print(e)
e = ResourceSaver.save("user://test.tscn", scene)
print(e)
func _set_owner(node, root):
if node != root:
node.owner = root
for child in node.get_children():
_set_owner(child, root)
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:
goat_save.goto_scene("user://test.tscn")
...
func goto_scene(path):
call_deferred("_deferred_goto_scene", path)
func _deferred_goto_scene(path):
get_tree().change_scene(path)
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 change_scene
, change_scene_to
, ResourceLoader
and a few other things, but I couldn’t find a proper solution. Can it be done in Godot Engine?
Cheers,
Paweł