CodeBlanks Logo Icon CodeBlanks

The Node concept

Learn that the Node is Godot's fundamental building block.

Node: the building block

In Godot everything starts with a node; behind every piece of the scene there is a node class. All node types derive from one common base class (Node) and share common properties such as name; subclasses like Node2D and Control add extra properties such as position and visible. In code you first create a new node with new and then attach it to the tree with add_child.

var oyuncu = Node2D.new()
oyuncu.name = "Oyuncu"
add_child(oyuncu)

Once it is attached to the tree, the node becomes part of the scene and starts working with the other nodes.

Node types and _ready

Node is the plainest node type; game objects are mostly built from Node2D, Node3D or, for interfaces, Control nodes derived from it. When a node is added to the tree and the scene starts, the _ready function runs once and you make your initial setup there. Nodes linked to each other as parent and child form the scene tree.

func _ready():
    print("Düğüm hazır: " + name)

The name property gives the node's name in the tree.

Practice Build a tree in code

Inside _ready build a small tree: create the kok node with Node.new() and name it "AnaSahne", create the oyuncu node with Node.new() and name it "Oyuncu", then add the oyuncu node under the kok node. Then print the name of kok and the name of its first child on two lines.

Starter code

Player.cs
func _ready():
    # 1) kok adında bir Node oluştur ve adını "AnaSahne" yap
    # 2) oyuncu adında bir Node oluştur ve adını "Oyuncu" yap
    # 3) oyuncu'yu kok'un altına ekle
    # 4) kok'un adını yazdır
    # 5) kok'un ilk çocuğunun adını yazdır
    pass

Sample solution

Player.cs
func _ready():
    var kok = Node.new()
    kok.name = "AnaSahne"
    var oyuncu = Node.new()
    oyuncu.name = "Oyuncu"
    kok.add_child(oyuncu)
    print(kok.name)
    print(kok.get_child(0).name)

Expected output

AnaSahne
Oyuncu

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which method is used to attach a newly created node to the scene's tree?

Player.cs
var oyuncu = Node2D.new()
oyuncu.name = "Oyuncu"
  • insert()
  • add_node()
  • attach()
  • add_child() Correct answer

Explanation: You add a node to the tree with add_child; before this call the node is not a part of the tree.

2 The node's name

Question
func _ready():
    print("Düğüm hazır: " + ____)
Answer
func _ready():
    print("Düğüm hazır: " + name)

Explanation: Every node keeps its name in the tree in the name property; you can join it with text and print it.

3 Node pieces

Term Meaning
Node The base class that all nodes build on
Node2D The node type for 2D game objects
add_child Adds a node to the tree
_ready Runs once when the node enters the scene

All lessons in Beginner