How to get the class name of a node in Godot 4: get_class() and get_global_name()
By Eduardo Orellana · Updated
node.get_class() returns the engine class, such as "Node2D". For the class_name of the node's script, use node.get_script().get_global_name(). To test a type, use is: node is Node2D. There is no class_name property: node.class_name stops with Invalid access to property or key 'class_name' on a base object of type '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
class_name is a keyword that declares a script's global name, not a property of objects. Reading it as a property looks for a property of that name and fails.
get_class() always returns the native class the object was created as. A script attached to it does not change that; Script.get_global_name() returns the script's class_name, or an empty name when it has none.
More on this: The "export" keyword was removed in Godot 4: every export(...) and its @export annotation
Reproduce it
A Node2D asked for a class_name property.
More on this: Invalid access to property or key on a base object of type 'Nil' in Godot 4
extends Node
func _ready() -> void:
var sprite = Node2D.new()
add_child(sprite)
print(sprite.class_name)
print("after")
What Godot 4.7.2 prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
SCRIPT ERROR: Invalid access to property or key 'class_name' on a base object of type 'Node2D'.
at: _ready (res://answers/how-to-get-the-class-name-of-a-node-in-godot-4/repro.gd:6)
GDScript backtrace (most recent call first):
[0] _ready (res://answers/how-to-get-the-class-name-of-a-node-in-godot-4/repro.gd:6)Fix it
get_class() for the engine class, is for a type test, and the script's own name.
extends Node
func _ready() -> void:
var sprite = Node2D.new()
add_child(sprite)
print(sprite.get_class())
print(sprite is CanvasItem)
print(get_class(), ", script: ", get_script().resource_path.get_file())
print("global name: '", get_script().get_global_name(), "'")
What the fixed script prints
More on this: Spatial in Godot 4: Could not find base class "Spatial"
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
Node2D
true
Node, script: fix.gd
global name: ''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_vm.cpp line 1128, the message](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L1128), [Object.get_class in the class reference](https://github.com/godotengine/godot/blob/4.7.2-stable/doc/classes/Object.xml), [Script.get_global_name in the class reference](https://github.com/godotengine/godot/blob/4.7.2-stable/doc/classes/Script.xml).
More on this: The "onready" keyword was removed in Godot 4: use @onready
Questions
- How do I check a node is a class I wrote?
- Give its script a class_name, then node is Enemy. is works with script classes the same as engine classes.
- What does is_class() do?
- It tests the engine class by name and its parents: node.is_class("CanvasItem"). It ignores scripts.
Related
- Invalid access to property or key on type 'Dictionary'
- Invalid cast: can't convert a non-object value to an object type
- Every verified Godot 4 answer
- Invalid assignment of property or key '…' with value of type '…' on a base object of type 'Node2D' in Godot 4
- Invalid access to property or key on a base object of type 'null instance' in Godot 4
- Invalid assignment of property or key with value of type on a base object of type 'Nil' in Godot 4
- Invalid access to property or key on a base object of type 'Nil' in Godot 4
- Spatial in Godot 4: Could not find base class "Spatial"
Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot.
Download Flockbay free