What is the SceneTree?
The SceneTree manages the active tree of nodes in your game. It’s automatically created when Godot starts and serves as the default MainLoop implementation, making it responsible for:- Managing the node hierarchy
- Processing node callbacks (
_process,_physics_process) - Handling scene loading and switching
- Managing node groups
- Controlling game pause state
- Providing access to the root node
Every node in your game can access the SceneTree using
get_tree(), but only if the node is currently in the tree.Accessing the SceneTree
The Root Node
The root node is the topmost node in the scene tree, always of type Window:Scene Management
The SceneTree provides several methods for loading, changing, and reloading scenes.Changing Scenes
1
Change Scene from File Path
The most common way to switch scenes:
2
Change Scene from PackedScene
Use a preloaded scene for faster switching:
3
Change Scene to Node Instance
Switch to an already instantiated scene:
When changing scenes, the current scene is removed from the tree immediately, then freed at the end of the frame. The new scene is added after the current frame completes.
Scene Change Order
Understanding the order of operations during scene changes is crucial:Reloading the Current Scene
Unloading a Scene
Pausing the Game
The SceneTree controls whether the game is paused:- Physics processing stops
_process()and_physics_process()may not be called (depends onprocess_mode)- Collision detection pauses
Process Modes
Nodes can control how they behave when the game is paused:Node Groups
Groups allow you to organize nodes by category and operate on multiple nodes at once.Adding Nodes to Groups
Nodes are automatically removed from all groups when freed. You rarely need to manually remove nodes from groups.
Querying Groups
Calling Methods on Groups
One of the most powerful features is calling methods on all nodes in a group:Group Call Flags
Node Paths
Node paths are used to reference nodes within the scene tree:Absolute paths start with
/root/ and work from the root node. Relative paths work from the current node.Scene Tree Timers
The SceneTree can create one-shot timers:Timer Parameters
Quitting the Game
On iOS, apps should not programmatically quit. The quit function is ignored on that platform.
Practical Example: Game Manager
Here’s a complete game manager that uses SceneTree features:Scene Tree Signals
The SceneTree emits useful signals:Best Practices
1
Use Groups for Categories
Organize nodes into logical groups (enemies, UI, collectibles) for easy batch operations.
2
Prefer Deferred Group Calls
Use
GROUP_CALL_DEFERRED when calling methods on groups to avoid issues with nodes being freed during iteration.3
Set Process Modes Appropriately
Make sure UI and pause menus use
PROCESS_MODE_WHEN_PAUSED so they work while the game is paused.4
Await scene_changed
When changing scenes, await the
scene_changed signal if you need to access the new scene immediately.5
Use Autoload for Game Management
Create autoload singletons that use SceneTree features to manage game state globally.
Next Steps
Nodes and Scenes
Learn about the building blocks managed by SceneTree
Resource System
Understand how Resources work with scenes