how to get the class name of a custom node

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Thakee Nathees

I have created a custom node like below

extends Area2D
class_name CollisionArea

func _ready():
    connect("area_entered", self, "on_area_entered")

func on_area_entered(var area):
    print( area.get_class() )

when ever 2 CollitionArea nodes collides it prints “Area2D” but I want the custom class name ( CollisionArea ) to be printed, is there any way to get the name? (i don’t want to use the node.get_name() because names often change)

Have you tried “Object.get_class()” ?

AlMoeSharpton86 | 2019-06-05 22:45

:bust_in_silhouette: Reply From: Thomas Karcher

get_class() always returns the name of the native class. It’s easy to change this behaviour by overriding the method in your custom node:

func get_class(): return "CollisionArea"

See https://github.com/godotengine/godot/issues/21789 for a discussion about this behaviour and ideas on how to change it in the future.