Game Development
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.
Exercise 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.
Show example solution
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)
Test yourself
-
Which method is used to attach a newly created node to the scene's tree?
var oyuncu = Node2D.new()
oyuncu.name = "Oyuncu"
- insert()
- add_node()
- attach()
- add_child()
Show answer
add_child() — You add a node to the tree with add_child; before this call the node is not a part of the tree.
-
The node's name
func _ready():
print("Düğüm hazır: " + ____)
Show answer
func _ready():
print("Düğüm hazır: " + name)
-
Node pieces
- 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
Next lesson: Saving the project