YSort in Godot 4: use y_sort_enabled on a Node2D
By Eduardo Orellana · Updated
Godot 4 has no YSort node. Any Node2D sorts its children by Y when its y_sort_enabled property is on, so a plain Node2D with y_sort_enabled = true replaces it. In a Godot 4 project, YSort.new() fails to parse with Identifier "YSort" not declared in the current scope. Did you mean to use "Node2D"?.
AppImage, x86_64. Ubuntu 22.04, Debian 12, Fedora 39 or newer.
Intel or AMD Chromebook, with Linux turned on.
What Godot is checking
GDScript resolves every name when the script loads. YSort is not a class in Godot 4, so the analyzer stops before anything runs, and because the name is in Godot's own table of Godot 3 renames, the message names the replacement.
The converter turns YSort into Node2D, and the table notes why: CanvasItem has a y_sort_enabled property. A TileMapLayer has it too, so tiles and characters can sort against each other.
Reproduce it
A Godot 3 script that creates a YSort in code, in a Godot 4 project.
extends Node
func _ready():
var sorter = YSort.new()
print(sorter)
What Godot 4.7.2 prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
SCRIPT ERROR: Parse Error: Identifier "YSort" not declared in the current scope. Did you mean to use "Node2D"?
at: GDScript::reload (res://answers/ysort-in-godot-4/repro.gd:4)
ERROR: Failed to load script "res://answers/ysort-in-godot-4/repro.gd" with error "Parse error".
at: load (modules/gdscript/gdscript_resource_format.cpp:46)Fix it
A Node2D with y_sort_enabled, holding two sprites.
extends Node2D
func _ready() -> void:
var world := Node2D.new()
world.y_sort_enabled = true
add_child(world)
for y in [80, 20]:
var s := Sprite2D.new()
s.position = Vector2(0, y)
world.add_child(s)
print("y sort: ", world.y_sort_enabled, ", children: ", world.get_child_count())
What the fixed script prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
y sort: true, children: 2Verified on Godot 4.7.2
Both scripts on this page were run headless on Godot 4.7.2 (4.7.2.stable.mono.official.ed1daf0bf) on 18 October 2026, and the output above is what Godot printed, unedited.
Where the check lives in Godot's source: [gdscript_analyzer.cpp line 4687, the message with its hint](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_analyzer.cpp#L4687), [renames_map_3_to_4.cpp line 1662, YSort to Node2D](https://github.com/godotengine/godot/blob/4.7.2-stable/editor/project_upgrade/renames_map_3_to_4.cpp#L1662), [Node2D class reference](https://github.com/godotengine/godot/blob/4.7.2-stable/doc/classes/Node2D.xml).
Questions
- Why does Y-sort not work for my character?
- Every node in the chain needs it: the parent that sorts must have y_sort_enabled, and so must any Node2D between it and the sprites that should sort.
- Which point is sorted?
- The node's origin. Put a character's origin at its feet so it sorts correctly against walls and trees.
Related
- Position2D in Godot 4: it is Marker2D now
- Sprite in Godot 4: it is Sprite2D now
- Every verified Godot 4 answer
- How to get the class name of a node in Godot 4: get_class() and get_global_name()
- RayCast in Godot 4: it is RayCast3D now
- ARVROrigin in Godot 4: it is XROrigin3D now
- ToolButton in Godot 4: use a flat Button
- Identifier "…" not declared in the current scope in Godot 4
Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot.
Download Flockbay free