scene manager system
This commit is contained in:
parent
aa6b9cb4aa
commit
cc177f7beb
6 changed files with 74 additions and 3 deletions
30
autoloads/scene_manager.gd
Normal file
30
autoloads/scene_manager.gd
Normal file
|
@ -0,0 +1,30 @@
|
|||
extends Node
|
||||
|
||||
|
||||
@export var scene_parent: Node
|
||||
|
||||
|
||||
var current_scene: Node
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
var tree = get_tree()
|
||||
# capture main scene
|
||||
if tree.current_scene != self:
|
||||
change_scene(tree.current_scene)
|
||||
tree.current_scene = self
|
||||
|
||||
|
||||
func change_scene(new_scene: Node) -> void:
|
||||
# avoid infinite recursion :P
|
||||
if current_scene == new_scene:
|
||||
return
|
||||
# remove current scene
|
||||
if current_scene:
|
||||
scene_parent.remove_child(current_scene)
|
||||
current_scene.queue_free()
|
||||
# add new scene
|
||||
if new_scene.is_inside_tree():
|
||||
new_scene.get_parent().remove_child.call_deferred(new_scene)
|
||||
scene_parent.add_child.call_deferred(new_scene)
|
||||
current_scene = new_scene
|
Loading…
Add table
Add a link
Reference in a new issue