How do I make a persistent scene?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By thewraithmod

I have an “overworld” where the player can walk around and find dungeons/towns. When a player is overlapping one of these locations they have the option to enter them. Doing so causes them to switch scenes (to the dungeon scene where a different player scene is used, for example)

When the player exits the dungeon I want them to be able to return to the “overworld” with everything staying saved/the way it was before they entered the dungeon.

In the “player_overworld.gd” script, I call the save_scene() function from the Global.gd singleton.

func _input(event):
if event.is_action_pressed("space"):
	GLOBAL.save_scene()

this is my code for saving the overworld scene and entering a dungeon:

func save_scene():
var root = get_tree().get_root()
saved_scene = root.get_child(root.get_child_count() -1)
get_tree().get_root().remove_child(saved_scene)
var new_scene = temp_scene.instance()
get_tree().get_root().add_child(new_scene)
get_tree().set_current_scene(new_scene)

This method is in a singleton called “Global.gd”

The variable “saved_scene” is also in that script.

Whenever this method is run I get a list of errors that all say:
0:00:08:0751 - Condition ’ !area_in && !E ’ is true.

I assume this is somehow related to the fact that the player_overworld scene is an Area2D node that checks for overlapping areas every frame. I do not know how to get rid of these errors and I am wondering if there is a better way to do scene switching and returning. I just want to have an overworld scene that is persistent and can safely be switched.

:bust_in_silhouette: Reply From: happycamper

I would save the data to a file instead of keeping it in memory. It might be a bit more work to set it up this way but its better in the long run. Check out the tutorial on saving and loading. Saving games — Godot Engine (3.0) documentation in English
Also I dont understand why you have a world as an area2D. What does your scene tree look like while you are in the overworld?

My world is not an area2D (it’s a Node2D). The player scene which i named “player_overworld” is an area2D node.

thewraithmod | 2018-07-13 05:38