+2 votes

Given when you make custom object class (plainly a script), how can you check with gdscript for its type?
Presumably you would use Object.is_type for the checks, but they never worked for me:

if myobject.is_type("myclass.gd"):

I've tried different combinations but without success. Also note that they changed the method in Godot3 to is_class().
Another way to check could be:

if myobject is mycalss:

But that one also doesn't to work quite as well.

asked Feb 28, 2018 in Engine by Guilherme Furst (55 points)

Not perfect at all, but you could use get_script() and regex, requiring the script name to be equal to the type name:

# obviously the pattern is not right for the script name, it's just two seperated examples
# you'll have to come up with a expression, that works
extends Node2D

func _ready():
    var script_path= get_script().resource_path
    var regex = RegEx.new()
    regex.compile("something")
    var string = "something1"
    var substring = regex.sub(string, "1")
    print(substring)

3 Answers

+3 votes
Best answer

I used to use meta data, or special flags, but lately I've been using another idea that I am favoring since it is more in line with Godot's internals.

I override the get_type() and is_type(type) methods in my classes.

It might look like this: (Though I generally use constants.)

func is_type(type): return type == "MyObject" or .is_type(type)
func    get_type(): return "MyObject"

In the is_type(type) it will also fall back to an inherited type. If your object extends a KinematicBody2D, you could still check for that with is_type("KinematicBody2D"). The first condition == "MyObject" would be false, then it would pass the parameter back to the original method for comparison, and if it matches will return true.

answered Feb 28, 2018 by avencherus (4,815 points)
selected Mar 1, 2018 by Guilherme Furst

I like this, good idea.

What I don't get is, what does get_type returns for custom classes? have you experimented there?

Okay, so just to be clear, I did some testing, and it seem get_type() really just returns the base class of what the object would be (the c++ class).

So doing a replacement function works pretty well.

It seems like get_type and is_type are get_class and is_class in 3.1 now. Other than that, yeah, this works finely~!

0 votes

Not perfect at all, but you could use getscript() and regex, requiring the script name to be equal to the type name (Object.resourcename returns nothing for some reason):

# obviously the pattern is not right for the script name, it's just two seperated examples
# you'll have to come up with a expression, that works
extends Node2D

func _ready():
    var script_path= get_script().resource_path
    var regex = RegEx.new()
    regex.compile("something")
    var string = "something1"
    var substring = regex.sub(string, "1")
    print(substring)
answered Feb 28, 2018 by Footurist (830 points)
+6 votes

GDScript resources behave like classes, so you can do it like this:

if my_object is preload("custom_class.gd"):

Or like this:

const CustomClass = preload("custom_class.gd")

and then:

if my_object is CustomClass:
answered Feb 28, 2018 by Zylann (26,157 points)

Zylann, do you happen to know why print(getscript().resourcename) prints nothing?

Clean solution, but that requires the class to be loaded already on the script.

@Zylann, I think probably because resourcename and resourcepath are different things, while most resources should have a path, they're aren't all named.

It requires to load the script indeed, but it's the closest to an OOP solution and it will have zero cost most of the time, unless you preload scenes or assets into that script (which isn't a good idea imo, load would be preferred)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.