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): if value == "MyCustomClass": return true else: return false
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.