Godot Engine – How to get the class name of a custom class

Last Updated on 1. October 2023 by Victor Karp

If you have ever created custom classes by using class_name MyCustomClass in one of your scripts you might have noticed that these classes don’t return their custom class name when you use get_class() or is_class() on them. This tutorial will show you two solutions to this problem.

Let’s say you have a Player.gd script that inherits from KinematicBody and you have added the line class_name Player to it. When you ask the player about his class by using get_class(), he’ll respond with the class he’s inheriting from instead, which is KinematicBody. Trying to use is_class(“Player”) will return false.

Solution 1: Use the “is” keyword

This solution was sent via mail by Simon. Check out his website and his articles about game art tricks!

Instead of using the get_class() and is_class() functions, you can check if an object is a certain class by using the is keyword, like so:

return my_object is MyCustomClass

This will return either true or false, and it works for custom classes as well. Make sure to use the actual class name (MyCustomClass in this example) without quotation marks around it.

Solution 2: Override get_class() and is_class()

Add the following lines of code to your custom class and replace MyCustomClass with the name of your own custom class:

func get_class():
    return "MyCustomClass"

func is_class(value):
    return value == "MyCustomClass"

This overrides the default get_class() and is_class() methods and returns your custom class name correctly.

Visit the Godot Tutorial main page for more Godot tutorials.

Scroll to top