Skip to content

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.

Download for macOSDownload for WindowsDownload for Linux

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.

More on this: Does Godot use Python? No: GDScript looks like Python, and here is where it is not

Reproduce it

A print() written at class level.

More on this: Identifier "…" not declared in the current scope in Godot 4

GDScript
extends Node

print("hello")

func _ready() -> void:
	pass

What Godot 4.7.2 prints

Output
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.

GDScript
extends Node

var greeting := "hello"

func _ready() -> void:
	print(greeting)

What the fixed script prints

Output
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org

hello

Run this fix with your agent

Flockbay is the Godot editor with an AI agent built in. Paste this into Flockbay's chat, or into Claude Code or Codex connected to Flockbay over MCP: the agent finds the cause in your project, applies the fix, runs the game headless and reads Godot's output to check it.

More on this: RigidBody in Godot 4: it is RigidBody3D now

Prompt
My Godot 4 project prints this error: "Parse Error: Unexpected identifier "print" in class body.". Find where it comes from and fix it. 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. Then run the game headless and check that Godot's output no longer shows it.

Verified 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.

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.

Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot.

Download Flockbay free