Last Updated on 17. November 2022 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 how to fix this.
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.
This makes custom classes much less useful than they could be. Until there’s an official redesign to this functionality, here’s how to fix it.
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 get_class() and is_class() methods and returns your custom class name correctly.
Visit the Godot Tutorial main page for more Godot tutorials.