TExcelTAD
Home › Godot 4 with GDScript › The Node Lifecycle

The Node Lifecycle

You never write a main loop in Godot. The engine creates your nodes, adds them to the tree and calls virtual methods on them at fixed moments. Knowing the order of those calls explains most "why is this null?" bugs.

Method When it runs Typical use
_init() when the object is created (new(), instantiate()), before it is in the tree default values; no access to children from a scene, no get_tree()
_enter_tree() each time the node enters the tree, parents before children rare; registering with a manager
_ready() once, after the node and all its children entered the tree: children before parents find child nodes, connect signals, initial setup
_process(delta) every rendered frame visuals, UI, timers you don't need to be exact
_physics_process(delta) every physics tick (60 per second by default) movement, collisions, anything gameplay-critical
_input(event) / _unhandled_input(event) for every input event see The Input Map & Input Events
_exit_tree() when the node leaves the tree, children before parents disconnect from managers, save state
_notification(what) for every notification, including NOTIFICATION_PREDELETE just before the object is freed low-level hooks

The order, measured

The probe below prints each callback. A small tree is built outside the tree first, then added in one go, as instantiate() + add_child() does with a scene:

gdscript · try it yourself
extends Node

# frames: 3

class Probe extends Node:
	func _init(n: String):
		name = n
		print("  _init        ", n)
	func _enter_tree():
		print("  _enter_tree  ", name)
	func _ready():
		print("  _ready       ", name)
	func _exit_tree():
		print("  _exit_tree   ", name)
	func _notification(what):
		if what == NOTIFICATION_PREDELETE:
			print("  predelete    ", name)

var level: Probe
var frame := 0

func _ready():
	print("building (not in the tree yet):")
	level = Probe.new("Level")
	var player := Probe.new("Player")
	player.add_child(Probe.new("Sprite"))
	level.add_child(player)
	level.add_child(Probe.new("Enemy"))
	print("adding Level to the tree:")
	add_child(level)

func _process(_delta):
	frame += 1
	print("frame ", frame)
	if frame == 1:
		level.get_node("Enemy").queue_free()
		print("  queued Enemy, Level still has ", level.get_child_count(), " children")
	if frame == 2:
		print("  Level now has ", level.get_child_count(), " children")
		var p := level.get_node("Player")
		level.remove_child(p)
		print("  removed Player, inside tree: ", p.is_inside_tree())
		p.free()
building (not in the tree yet):
  _init        Level
  _init        Player
  _init        Sprite
  _init        Enemy
adding Level to the tree:
  _enter_tree  Level
  _enter_tree  Player
  _enter_tree  Sprite
  _enter_tree  Enemy
  _ready       Sprite
  _ready       Player
  _ready       Enemy
  _ready       Level
frame 1
  queued Enemy, Level still has 2 children
  predelete    Enemy
  _exit_tree   Enemy
frame 2
  Level now has 1 children
  _exit_tree   Sprite
  _exit_tree   Player
  removed Player, inside tree: false
  predelete    Player
  predelete    Sprite
frame 3
  _exit_tree   Level
  predelete    Level

What to take from it:

  • _enter_tree goes top-down, _ready goes bottom-up. When a parent's _ready runs, every child is already ready, so a parent can safely use its children. A child's _ready can't rely on its parent being ready.
  • _ready runs once. Removing a node and adding it again calls _enter_tree again but not _ready, unless you call request_ready().
  • queue_free() is deferred. The Enemy is still a child for the rest of frame 1 and is deleted at the end of the frame. That makes it safe to call from anywhere, including from the node's own signal handlers and physics callbacks. free() deletes immediately, which crashes code that still holds the node later in the same frame.
  • Removing is not freeing. remove_child() takes a node out of the tree (_exit_tree runs) but keeps it in memory; you can add it elsewhere (or use reparent()). A removed node you never free is a memory leak.
  • At the end, quitting frees the whole tree, so the remaining Level exits too.

_process vs _physics_process

Both run every frame of their own clock. Physics ticks at a fixed rate (Project Settings > Physics > Common > Physics Ticks per Second, 60 by default), while _process follows the display. On a 144 Hz monitor _process runs 144 times a second and _physics_process still 60. Within one frame, physics ticks run first. The order between nodes is the tree order (parents before children, siblings top to bottom), and process_priority (lower first) overrides it for _process only:

gdscript · try it yourself
extends Node

# frames: 2

class Ticker extends Node:
	func _init(n: String, priority := 0):
		name = n
		process_priority = priority
	func _physics_process(_delta):
		print("  physics ", name)
	func _process(_delta):
		print("  process ", name)

func _ready():
	add_child(Ticker.new("A"))
	add_child(Ticker.new("B"))
	add_child(Ticker.new("C", -1))
	var off := Ticker.new("Off")
	add_child(off)
	off.set_process(false)
	off.set_physics_process(false)

func _process(_delta):
	print("frame ", Engine.get_process_frames())
  physics A
  physics B
  physics C
  process C
frame 0
  process A
  process B
  physics A
  physics B
  physics C
  process C
frame 1
  process A
  process B
  • C has process_priority = -1, so its _process runs before everyone else's, even its parent Main. Physics order is controlled separately by process_physics_priority.
  • Off never prints: set_process(false) and set_physics_process(false) switch a node's callbacks off. Godot also only calls _process on scripts that define it, so an empty _process is not free; delete it.
  • To pause whole branches of the tree (a pause menu), set get_tree().paused = true and give the menu process_mode = PROCESS_MODE_ALWAYS.

Common lifecycle bugs

Symptom Cause Fix
Invalid access to property or key 'text' on a base object of type 'Nil' in _init children of a scene don't exist yet in _init do it in _ready, or use @onready (lesson)
get_tree() returns null called before the node is in the tree move the code to _ready or _enter_tree
A property set after add_child is ignored by _ready _ready ran during add_child set properties before add_child
Movement is jittery or speed depends on FPS movement in _process without delta move in _physics_process and scale by delta
"Can't change this state while flushing queries" adding or removing physics nodes inside a physics callback call_deferred("add_child", node) or set_deferred

Test yourself

In which order does _ready run for a parent and its two children?

  1. Parent, then children
  2. Children first, then the parent
  3. Random order
  4. Only the parent's _ready runs

Why is queue_free() preferred over free()?

  1. It is faster
  2. It deletes the node at the end of the frame, so code still using it in this frame doesn't crash
  3. It keeps the node in memory forever
  4. It only works on 3D nodes

Physics ticks at 60 Hz and the monitor refreshes at 144 Hz. How often does _physics_process run per second?

  1. 144
  2. 60
  3. 204
  4. It depends on the scene size