'(Godot) They are also nodes of objects

I want to understand if for example a game object like a SPRITE that if inserted alone represents a scene in godot is related to the concept of OOP object, in practice I want to understand if it is also an OOP object....



Solution 1:[1]

In Godot, a node is an object. Not all objects are nodes.


We can see the documentation for Node, that it inherits from Object, where Object is the base class for everything in Godot.

And, of course, Sprite is a Node.

So, all Sprites are Nodes, and all Nodes are Objects.


What is a scene?

First of all, scenes are not C++ classes. I hope this is evident.

Second, there is something called scene inheritance, which suggest scenes are some kind of classes. However, we cannot extend them in code. Scenes are not classes.

Third, we can create Nodes from scene files. If the scene file is not a class, that suggest they are prototypes. But if scenes are prototypes, how do we explain scene inheritance? Well, as it turns out, scenes are not prototypes. It is much simpler than that…

Scenes are Nodes.


As you know, when you tell Godot to add a scene file to the Scene panel you get an item that has a special icon. And - by default - you don't see any children that where in that scene file. This is only they way they are shown in the UI.

Perhaps you wonder how does it look like in memory. Is there a special type of Node for any scene files you add? And the answer is NO. They are regular Nodes.

When you tell Godot to add a scene file to the Scene panel, Godot loads it and instantiate it, getting a Node. Then Godot adds that Node as a child where you told it.


So how does it remember that that Node was added from a scene file?

The Nodes have a filename property that tell us from where it was loaded. So when you are serializing a Node to an scene file, and it has some child Node that has a filename set, instead of serializing that child Node to the same scene file... It is serialized to the file indicated by its filename, and then included by reference.

Of course, the filename property is not visible in the Inspector panel.


But then, what is scene inheritance?

When you have a serialized scene Node (i.e. a scene file), it contains the properties, scripts, children, connections and so on. And as said above, it can include other files by reference.

Scene inheritance is the special case of inclusion where the serialized Node is defined as another scene. That is scene inheritance is extension by inclusion.


Finally there is something called SceneTree. The game will have an SceneTree object (unless you instantiate more), and it references a Node which is considered the current scene.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1