Refactor Scene management #1

Open
opened 2026-05-26 21:31:23 +02:00 by fbouju · 5 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
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.