
[ad_1]
what should the parent of the root Instance in the hierarchy be?
Nothing.
Does Instance
s without parent make sense?
If you, for example, might want a factory that create Instance
, then set their properties, perhaps compose them in some way, and then attach them to the scene tree…
Or if you, for another example, might want to be able to remove an Instance
from the scene tree, manipulate it in some way, and the reattach it to the scene tree, perhaps in a different place…
Then it make sense that an Instance
can have no parent. More precisely that an Instance
can either have zero or one parent, and that the parent can change in runtime.
Should the root of the scene tree be something special?
It could be an object dedicated to just being the root.
However, often the root often has other functions, such as handling where the game will be rendered to (Window, Canvas, Viewport…). Furthermore, it might make sense to have multiple of those objects (e.g. a main window with children windows).
Although, that might not apply to your engine.
I remind you that even though the scene tree has a root, there will be another code that uses it. And so it will have a reference to its root. Thus, you might have a SceneTree
class (which is not an Instance
and thus has no parent) that has an Instance
which is considered to be the root of the scene tree.
The problem then becomes: How do you guarantee that the root of the scene tree has no parent?
Before we get to that, here is a related question: How do you guarantee that an Instance
does not have two parents? That has two parts:
- There is only one parent pointer, so it can only have one parent.
- When you are going to add a child
Instance
you check if it has a parent already.
How do you guarantee that the root of the scene tree has no parent?
Well, similar logic applies to the scene tree root:
- When you set a
Instance
as the root of the scene tree, you check it does not have a parent. - When are going to add a child
Instance
you check also if it is the root of the scene tree.
But I’m skipping a question…
Should you be able to change the scene tree root?
If the scene tree root is the scene root, then yes. You should be able to change it, because you should be able to change scenes. Thus, not making the scene root the scene tree root allows you create the root of the scene tree during initialization and never change it.
Thus the first point above (checking if the Instance
you will set as root of the scene tree has a parent before setting it) would not be necessary.
And the second is easier (checking if an Instance
is the root of the scene tree) because anybody could access the root of the scene tree via the composition root to compare.
[ad_2]