The SceneGraph is an optional tool that can be used to build our games. It handles creation, updates, adding and removing nodes, and even has an optional UI portion. This can be very useful if we are familiar with how the tree structure works in game engines such as Godot or Unity. This particular implementation is heavily influenced by the scene tree in Godot.
The scene graph itself is quite simple. It contains a single root Node that it manages and invokes it’s
update() method. Whenever we add a new node to the tree, it will get added directly to this root
Node. The root
Node will then invoke the
update() method unto it’s children and so on and so forth.
The scene graph also contains a root
Camera which is then used subsequently by any of the nodes on the tree. When a
Node is part of the
SceneGraph the instance of the graph can be obtained by accessing the
When a node is connected directly or indirectly to the root
Node, it becomes part of the scene graph. The
Node will have certain lifecycle callbacks invoked upon entering (and upon leaving).
Order of lifecycle methods of a
Node when added to a
onAddedToScene(): Called when this
Nodeis added to a
SceneGraphafter all pending
Nodechanges are committed.
ready():Called when this
Nodeand all of it’s children are added to the scene and active
onPostEnterScene(): Called when this
Nodeand all of it’s children are added to the scene and have themselves called
Node operations are done in tree order, meaning parents and siblinings with a lower rank in the tree will get notified before the current node.
“Becoming active” when entering the
- The root of the scene is created and initialized.
- Every node of the newly added scene will recevie that
onAddedToScene()callback in top-to-bottom order.
- A second callback
onPostEnterScene()is also invoked once all of its children inside the scene and active, from bottom-to-top order.
- An extra callback
ready()is invoked when a node all of its children are inside the scene.