Does a hierarchical relationship between nodes represent a "has-a" or "is-a" relationship, or both?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By salbert

The editor, as well as the official tutorial, seems to imply that, when one node is a child of another, that child inherits the properties of its parents in an object oriented way. This intuitively makes sense.

However, the editor also allows me to add a child to a parent node that, according to the documentation, does not share a lineage.

For example, I’ve created a Panel node as a root node. Its inheritance tree is such:
Control < CanvasItem < Node < Object

Underneath it, I have given it a Path2D node as a child. Its inheritance tree is
Node2D < CanvasItem < Node < Object

Both nodes are CanvasItems, but their direct descendants are different.

So am I correct in assuming that, while it’s not true that hierarchical relationships between nodes always necessarily represent an “is-a” relationship, they do always represent a “has-a” relationship?

I’m essentially trying to understand how a scene hierarchy would be represented if typed out in source code.

Thank you.

:bust_in_silhouette: Reply From: 2D

Your assumption is correct, BUT don’t confuse the scene tree with the inheritance tree. The scene tree has the following main functionality:

  1. Ensure the function callbacks of tree members get called each tick
  2. Rendering
  3. Organization
  4. Positioning
  5. etc.

For an Object (Base class for all entities in Godot) to be added as a child into the scene tree, it must inherit from Node.

That’s it.

So, only “is-a” Node must be met. The parent now “has-a” child of that Node type. This means absolutely nothing inheritance-wise for the child - it only gains scene tree functionality from the parent. (5) above - this provides the child with the get_parent(), get_node(), etc. functions for calling other nodes in the tree

A node in a scene tree can create a new Object that does not inherit from Node, but these cannot be added to the tree and can only be called by other objects through references.

If typing the scene hierarchy out in code, you will access the functions of the Node class such as get_node(), get_path(), get_tree(), etc. if you don’t already hold references to the nodes you are looking for. These take either a NodePath, string (Node name), or int (child idx).

Let us know if it is still not clear =)

So it’s pretty much what I expected.

The only inconsistency to me, as a beginner, is that there do seem to be some (perfectly logical) exceptions, such as CanvasItems’ children inheriting their parent’s transformations if they are also CanvasItems.

Thank you for the clarification!

salbert | 2020-01-01 02:11