+19 votes

Like the subject says, Is there a function in gdscript that works like the type() function in python? By that I mean a function that is able to return the type of a given object.

I would like to use this to make tests and check what type an object is in my script. I have a hard time knowing if I work with a rect2 or a vector2 or something else at any given time, just learning you know =)

Maybe another way to check the type of an object easily?

in Engine by (22 points)

Can anyone update this for Godot 3.0, which no longer has obj.get_type()?

6 Answers

+11 votes

For objects there is get_type() and for built in types there is typeof()

http://docs.godotengine.org/en/stable/classes/class_object.html#class-object-get-type

http://docs.godotengine.org/en/stable/classes/class_@gdscript.html#class-gdscript-typeof

get_type() returns a string of the node type, and typeof() returns an integer of that type, which can be compared against a constant such as TYPE_INT, etc.

print(node.get_type())
print(typeof(value) == TYPE_INT)

A list of constants are found here:

http://docs.godotengine.org/en/stable/classes/class_@global%20scope.html

by (5,245 points)
edited by

I would add that get_type() is not only for nodes, but every instance that inherits Object (Nodes, Resources, custom GDScript classes...)
You can also use if obj extends Class to test if a type is or extends a specific class.
Note that preload("some_script.gd") is also considered a class when using extends.
Also note that you can't use extends or get_type() on non-object types, so if you want to be sure you should check if typeof(obj) == TYPE_OBJECT first.

Oops, meant to write Object there. Thanks for pointing that out, I will correct it.

It would seem that get_type() was removed in recent versions. This answer may need updating

This answer has not been updated, but the right answer is below. get_class() seems to have replaced it.

0 votes

A generic form of type_of, if that would be helpful.

func general_type_of(obj):
var typ = typeof(obj)
var builtin_type_names = ["nil", "bool", "int", "real", "string", "vector2", "rect2", "vector3", "maxtrix32", "plane", "quat", "aabb",  "matrix3", "transform", "color", "image", "nodepath", "rid", null, "inputevent", "dictionary", "array", "rawarray", "intarray", "realarray", "stringarray", "vector2array", "vector3array", "colorarray", "unknown"]

if(typ == TYPE_OBJECT):
    obj.type_of()
else:
    builtin_type_names[typ]
by (14 points)
+15 votes

In Godot 3.0 you can test if a node is a given built in type: bool is_class ( String type ) const

entity.is_class("Node2D") #returns true

by (689 points)
+10 votes

Found it -- in godot 3.0 this is obj.get_class().

by (48 points)
+4 votes

as of November 2020 : using Godot 3.2.3


is : Gdscript keyword



To check custom types, use the keyword is.

      example : 
----------------------------
 # MyCustomClass.gd
 # attach to a node in the Scene Tree of type Control
----------------------------
extends Node
class_name MyCustomClass

func print_name():
    print( "Calling MyCustomClass" )
func print_class():
    print( "get_class() == ", get_class() )

----------------------------
# SomeOtherScript.gd
----------------------------
func how_to_use_keyword_is( some_var ):
    if some_var is MyCustomClass: 
        some_var.print_name()        #prints: Calling MyCustomClass
        some_var.print_class()       #prints: get_class() == Control

Important to note
Even though MyCustomClass.gd has a line that says extends Node, Godot knows that this script is attached to a Control node in the scene, and that's what get_class() returns. You can see the type by hovering over a node in the Scene Tree.

Second important note
You cannot check a script's type within that script using self and is. Don't use this code in Godot 3.2.3, but adding here in case it helps search results.

----------------------------
 # MyCustomClass.gd
----------------------------
class_name MyCustomClass

                 ...

func this_does_not_work_currently():
    if self is MyCustomClass : 
        print("I wish this was implemented.")

# If you try this comparison, 
# Godot 3.2.3 will throw an error that says:
# 
# Parser Error: Using own name in class file is not allowed (creates a cyclic reference)



Doc page for is :
https://docs.godotengine.org/en/3.2/getting_started/scripting/gdscript/gdscript_basics.html#inheritance


typeof() : Gdscript function


typeof(some_var) is for builtin variant types only (see Hyper Sonic's comment above)


Object.get_class() : useful, but with caveat


Basics

Use Object.get_class() to check the type of node a script is attached to. It returns the type you see in the scene tree, not the type you inherit from with extends (see example above).

Alternatively, if a script extends nothing,
get_class() will return a string that says "Reference".

From https://docs.godotengine.org/en/stable/getting_started/scripting/gdscript :
    "By default, all classes that don't define inheritance extend Reference."

Nuances

However, get_class() does not check for a custom class_name defined at the beginning of a script, and returns the built-in inherited type.

If you want to check for a custom class_name, use is.

  • If you attach a script to a node of type Node,
    type extends Node at the top,
    -> get_class() will return "Node".

  • If you attach a script to a node of type Control,
    type extends Node at the top,
    -> get_class() will return "Control".

  • If you attach a script to a node of type Control,
    type extends Control
    followed by class_name CustomClass,
    -> get_class() will return "Control".

  • If a script has no extends line,
    and you type class_name CustomClass
    -> then get_class() will return "Reference".

Workaround

From this answer by avencherus https://godotengine.org/qa/24745/how-to-check-type-of-a-custom-class?show=24756#a24756

If you want to use get_class() for custom class_names,
override the inherited Object.get_class() with a custom one in your script.

Keep in mind that if you do this, you lose the ability to check the type of the node the script is attached to.

----------------------------
 # MyCustomClass.gd
----------------------------
class_name MyCustomClass

#  override 
func get_class() : 
    return "MyCustomClass"

#  or use this instead and retain functionality of both
func get_class_name() : 
    return "MyCustomClass"


DEPRECATED: Object.get_type() is no longer in Godot 3.0+, only present in Godot 2


by (36 points)
edited by

Just to add to this, is also works on built-in types (#21449).

true is bool == 10 is int == "a" is String == Node2D.new() is Node == true

works on v3.1.x, v3.2.x, d39f638 . Highly possibly on v4.0 too.

So any good idea to check a script's type in itself?

Just implements get_class(), no better way that I know of.

func get_class():
    return "CustomClass"

a.get_class() == self.get_class()
–1 vote

Looking for new updates. Hope it will be launched soon.

by (54 points)
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.