Refactor Scene management #1

Closed
opened 2026-05-26 21:31:23 +02:00 by fbouju · 7 comments
Owner

Scene managment system

Every minigame scene is loaded one after another with the loadNextScene() function.
This function is implemented independently in each minigame main script.

Eg:

func loadNextScene():
	var currentScene = get_tree().root.get_child(3).get_child(1)
	var nextScene = load("res://scenes/shopping.tscn").instantiate()
	get_tree().root.get_child(3).add_child(nextScene)
	get_tree().root.get_child(3).remove_child(currentScene)
	currentScene.queue_free()

The plan is to create a global scene handler.

2 possible ways i see :

  • Create a loadScene(scenePath) function
  • Create a queue of scenes and a function loadNextScene() that gets the top of the queue and loads it

The scene handler could use a signal so that minigames can notify when to load the next scene.

Files impacted :

  • cutMirror.gd
  • meltSand.gd
  • painting_canvas.gd
  • runeGame.gd
  • smashGlass.gd
  • start_scene.gd
  • shopping.gd
### Scene managment system Every minigame scene is loaded one after another with the `loadNextScene()` function. This function is implemented independently in each minigame main script. Eg: ``` func loadNextScene(): var currentScene = get_tree().root.get_child(3).get_child(1) var nextScene = load("res://scenes/shopping.tscn").instantiate() get_tree().root.get_child(3).add_child(nextScene) get_tree().root.get_child(3).remove_child(currentScene) currentScene.queue_free() ``` The plan is to create a global scene handler. 2 possible ways i see : - Create a `loadScene(scenePath)` function - Create a queue of scenes and a function `loadNextScene()` that gets the top of the queue and loads it The scene handler could use a signal so that minigames can notify when to load the next scene. Files impacted : - `cutMirror.gd` - `meltSand.gd` - `painting_canvas.gd` - `runeGame.gd` - `smashGlass.gd` - `start_scene.gd` - `shopping.gd`
fbouju self-assigned this 2026-05-26 21:31:23 +02:00
fbouju added this to the Refactoring project 2026-05-26 21:31:23 +02:00
Owner

Create a queue of scenes and a function loadNextScene() that gets the top of the queue and loads it

I think this is the way to go as it would be a pain to change the order of the scenes if we add one in the middle of it for example

> Create a queue of scenes and a function loadNextScene() that gets the top of the queue and loads it I think this is the way to go as it would be a pain to change the order of the scenes if we add one in the middle of it for example
Author
Owner
### Ressources - https://docs.godotengine.org/en/stable/tutorials/scripting/singletons_autoload.html#custom-scene-switcher - https://github.com/godotengine/godot-docs-project-starters/releases/download/latest-4.x/singleton_autoload_starter.zip - https://docs.godotengine.org/en/stable/classes/class_scenetree.html#class-scenetree-method-change-scene-to-file - https://docs.godotengine.org/en/stable/tutorials/io/background_loading.html - https://www.reddit.com/r/godot/comments/1j86m79/best_practices_for_scene_management_in_godot/ - https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-call-deferred
Author
Owner

Commit a57a973d16

Changelog:

  • Added a signal sceneEnded() to signals.gd
  • start_scene.gd emits sceneEnded
  • Added sceneHandler script that connects sceneEnded to trigger the get_tree().change_scene_to_file(res://...)
Commit a57a973d16 ### Changelog: - Added a `signal sceneEnded()` to signals.gd - `start_scene.gd` emits `sceneEnded` - Added sceneHandler script that connects sceneEnded to trigger the `get_tree().change_scene_to_file(res://...)`
Owner

As you might have seen, this creates lots of things to change since the "scene" doesnt change in the current state, it removes and child and adds another scene as a child, this allows to keep the UI consistent throughout the gameplay/music/pause menu etc

Maybe the simplest way might be to do get_tree().get_child().change_scene_to_file() with a number (2 or 3 depending on how many global variables are declared) given to get_child()

I'll check it out tomorrow

As you might have seen, this creates lots of things to change since the "scene" doesnt change in the current state, it removes and child and adds another scene as a child, this allows to keep the UI consistent throughout the gameplay/music/pause menu etc ~~Maybe the simplest way might be to do `get_tree().get_child().change_scene_to_file()` with a number (2 or 3 depending on how many global variables are declared) given to `get_child()`~~ I'll check it out tomorrow
Owner

Commit 91badf90cc

Changelog:

  • Fixed issue #2 : removed the audioPlayers as children of "PauseMenu" scene, and added them to Global instead (It might be better to create a new global for all the audio related variables but would require to change lots of lines, will stay like this for now since I'm not sure this commit is best practice)
  • Added the GameInterface scene to shopping.tscn and meltSand.tscn, I did it manually but it could be added programmatically and it might be better. (This allows us to still use the pause menu and to actually have a game interface : The progress bar and the money amount for example)
  • Added some scene paths to scenePaths[] in sceneHandler.gd to test my system, an index that starts at 1 (0 is the main menu) is incremented each time after switching to next scene from _on_scene_ended(), if index overflows, it goes back to 1 (the scene with the witches)

After some experimentation, I think the GameInterface scene should be added as a child to a global node on the launch of the game instead of as a sibling or child in a scene. I'm not entirely sure so if it's alright with you we'll talk about it.

My work is getting messy so I won't push it yet but I'll probably work on it tomorrow. In any case I think this is mostly solved

Commit 91badf90cc ### Changelog: - Fixed issue #2 : removed the audioPlayers as children of "PauseMenu" scene, and added them to Global instead (It might be better to create a new global for all the audio related variables but would require to change lots of lines, will stay like this for now since I'm not sure this commit is best practice) - Added the `GameInterface` scene to `shopping.tscn` and `meltSand.tscn`, I did it manually but it could be added programmatically and it might be better. (This allows us to still use the pause menu and to actually have a game interface : The progress bar and the money amount for example) - Added some scene paths to `scenePaths[]` in sceneHandler.gd to test my system, an index that starts at 1 (0 is the main menu) is incremented each time after switching to next scene from `_on_scene_ended()`, if index overflows, it goes back to 1 (the scene with the witches) After some experimentation, I think the GameInterface scene should be added as a child to a global node on the launch of the game instead of as a sibling or child in a scene. I'm not entirely sure so if it's alright with you we'll talk about it. My work is getting messy so I won't push it yet but I'll probably work on it tomorrow. In any case I think this is mostly solved
Owner

Commit a50cedd04c

I think I finished this

Changelog :

  • Added a global UI class, globalUserInterface.gd containing :
    • Music related nodes and functions,
    • Mouse Cursor related functions and definitions,
    • Game Interface scene loading on launch (contains pause menu) and AudioStreamPlayers as children of this global node
  • Migrated relevant functions, variables and definitions to the globalUserInterface.gd file.
  • Migrated all relevant function and object references in all scene scripts.
  • Changed all scene scripts to use the Signals.sceneEnded.emit(), most of those calls (all minigames ones) are contained in a function finishScene() allowing us to put some timers, other signals, or some stuff before switching scene more easily in the future.
  • Added all scenes paths to sceneHandler path variable
  • Added a signal updateProgressBarVisibility(visible:bool, progress:int) to update the mirror making progress bar, emited by Globals.craft() and Globals.stopCrafting() to show and hide the progressBar respectively, as well as Globals.craftSuccess() to update the progress of the progressBar.

If this is all you had in mind for the sceneHandler for now, I think this issue can be closed.

Commit a50cedd04c I think I finished this ### Changelog : - Added a global UI class, `globalUserInterface.gd` containing : - Music related nodes and functions, - Mouse Cursor related functions and definitions, - Game Interface scene loading on launch (contains pause menu) and AudioStreamPlayers as children of this global node - Migrated relevant functions, variables and definitions to the `globalUserInterface.gd` file. - Migrated all relevant function and object references in all scene scripts. - Changed all scene scripts to use the `Signals.sceneEnded.emit()`, most of those calls (all minigames ones) are contained in a function `finishScene()` allowing us to put some timers, other signals, or some stuff before switching scene more easily in the future. - Added all scenes paths to sceneHandler path variable - Added a signal `updateProgressBarVisibility(visible:bool, progress:int)` to update the mirror making progress bar, emited by `Globals.craft()` and `Globals.stopCrafting()` to show and hide the progressBar respectively, as well as `Globals.craftSuccess()` to update the progress of the progressBar. If this is all you had in mind for the sceneHandler for now, I think this issue can be closed.
Author
Owner

Very good job !
Closing the issue

Very good job ! Closing the issue
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
abgit/MirrorMirror#1
No description provided.