paratate/globals/scene_stack.gd

36 lines
881 B
GDScript

extends Node
## The stack of scenes.
var scenes: Array[Node]
func _enter_tree() -> void:
scenes.push_back(get_tree().current_scene)
## Pushes a new scene onto the stack. It will replace the current scene until it is popped.
func push_scene(scene: Node) -> void:
scenes.push_back(scene)
get_tree().current_scene = scene
get_tree().scene_changed.emit()
## Pops the current scene off the stack, returning to the scene below it. If there are no
## scenes left, the game quits.
func pop_scene() -> void:
scenes.pop_back().queue_free()
if scenes.is_empty():
get_tree().quit()
return
get_tree().current_scene = scenes.back()
get_tree().scene_changed.emit()
## Swaps the current scene with a new scene.
func swap_scene(scene: Node) -> void:
scenes.pop_back().queue_free()
scenes.push_back(scene)
get_tree().current_scene = scene
get_tree().scene_changed.emit()