CodeBlanks Logo Icon CodeBlanks

The scene tree

Learn how a scene is organized as a tree in Godot.

A scene is a tree

In Godot every scene is arranged as a tree, and each part of the tree is called a node. In the Scene panel the root node appears at the top and its children are listed under it, nested. By dragging a node under another node you change the hierarchy.

func _ready():
    print("AnaSahne")
    print("  Oyuncu")
    print("    Silah")

This output shows that the Silah node sits under Oyuncu, and Oyuncu sits under AnaSahne.

Node paths

You can also reach every node in the tree by its path; a path joins the node names from the root with /. A node keeps its own name in the name property, and the node right above it comes from the get_parent() call. This way you can compute a node's place in the tree in a single line.

func _ready():
    print(get_parent().name + "/" + name)

If the Oyuncu node sits under AnaSahne, this line prints AnaSahne/Oyuncu.

Practice Build a tree path

Inside _ready, define three variables: kok = "AnaSahne", oyuncu = "Oyuncu" and silah = "Silah". On the first line print the three names joined with / after the label Yol: , and on the second line print the root after the label Kök: .

Starter code

Player.cs
func _ready():
    # 1) kok değişkenini "AnaSahne" olarak tanımla
    # 2) oyuncu değişkenini "Oyuncu" olarak tanımla
    # 3) silah değişkenini "Silah" olarak tanımla
    # 4) "Yol: " etiketiyle kok/oyuncu/silah birleşimini yazdır
    # 5) "Kök: " etiketiyle kok değerini yazdır
    pass

Sample solution

Player.cs
func _ready():
    var kok = "AnaSahne"
    var oyuncu = "Oyuncu"
    var silah = "Silah"
    print("Yol: " + kok + "/" + oyuncu + "/" + silah)
    print("Kök: " + kok)

Expected output

Yol: AnaSahne/Oyuncu/Silah
Kök: AnaSahne

Questions and answers

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

1 In Godot, how are the pieces of a scene organized?

  • As a tree; every piece is called a node Correct answer
  • As a grid; every piece sits in a cell
  • As a folder structure; every piece is a separate file
  • As a flat list; every piece gets a number

Explanation: Scenes are trees; there is one root node and the other nodes are ordered under it as children.

2 Calling the parent node

Question
func _ready():
    print(____).name + "/" + name)
Answer
func _ready():
    print(get_parent()).name + "/" + name)

Explanation: The node above is obtained with get_parent(); .name gives that node's name. get_child(0) instead goes down to the first child node.

3 Tree terms

Term Meaning
Kök düğüm Topmost node in the tree
Çocuk düğüm Node placed under another node
Düğüm yolu Path from root to target node
Hiyerarşi Parent-child node structure

All lessons in Beginner