Unexpected identifier "…" in class body in Godot 4
By Eduardo Orellana · Updated
A statement is written at the top level of the script, outside any function, where only declarations are allowed. print("hello") under extends Node fails to parse with Unexpected identifier "print" in class body. Move it into a function such as _ready(), or indent it under the function it belongs to.
AppImage, x86_64. Ubuntu 22.04, Debian 12, Fedora 39 or newer.
Intel or AMD Chromebook, with Linux turned on.
What Godot is checking
At class level GDScript accepts declarations only: extends, class_name, var, const, signal, enum, func, class and annotations. A call, an assignment or an if there is not a declaration, and the parser stops at its first token.
The usual cause is indentation: a line meant to be inside a function lost its indent and fell out to class level.
Reproduce it
A print() written at class level.
extends Node
print("hello")
func _ready() -> void:
pass
What Godot 4.7.2 prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
SCRIPT ERROR: Parse Error: Unexpected identifier "print" in class body.
at: GDScript::reload (res://answers/unexpected-identifier-in-class-body/repro.gd:3)
ERROR: Failed to load script "res://answers/unexpected-identifier-in-class-body/repro.gd" with error "Parse error".
at: load (modules/gdscript/gdscript_resource_format.cpp:46)Fix it
The statement inside _ready(), and a member variable initialised at class level, which is allowed.
More on this: setget in Godot 4: Expected end of statement after variable declaration, found "Identifier" instead
extends Node
var greeting := "hello"
func _ready() -> void:
print(greeting)
What the fixed script prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
helloVerified 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_parser.cpp line 1215, the message](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_parser.cpp#L1215).
More on this: Does Godot use Python? No: GDScript looks like Python, and here is where it is not
Questions
- Can a var at class level call a function?
- Yes, in its initialiser: var start_time := Time.get_ticks_msec() is a declaration, so it is allowed.
- What does the wording "Unexpected Identifier" in older posts mean?
- The same check. Godot 4.7.2 names the token: Unexpected identifier "print" in class body.
Related
- Expected indented block after function declaration
- Expected end of statement after variable declaration
- Every verified Godot 4 answer
- Does Godot use Python? No: GDScript looks like Python, and here is where it is not
- setget in Godot 4: Expected end of statement after variable declaration, found "Identifier" instead
- Identifier "…" not declared in the current scope in Godot 4
- Cannot get return value of call to "…()" because it returns "void" in Godot 4
- The "tool" keyword was removed in Godot 4: use @tool
Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot.
Download Flockbay free