Is there a corresponding type() function in gdscript?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By thurines
:warning: Old Version Published before Godot 3 was released.

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?

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

garyo | 2018-05-14 21:01

:bust_in_silhouette: Reply From: avencherus

For objects there is get_type() and for built in types there is 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

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.

Zylann | 2017-03-09 13:37

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

avencherus | 2017-03-09 14:46

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

Aristonaut | 2019-12-18 01:18

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

greenfox1505 | 2019-12-28 21:10

And the new links, in the same order:

Object.get_class()
Object — Godot Engine (stable) documentation in English

typeof()
@GDScript — Godot Engine (stable) documentation in English

enum Variant.Type possible values
@GlobalScope — Godot Engine (stable) documentation in English

Hyper Sonic | 2020-02-27 23:40

:bust_in_silhouette: Reply From: Jengamon

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]
:bust_in_silhouette: Reply From: jarlowrey

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

:bust_in_silhouette: Reply From: garyo

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

:bust_in_silhouette: Reply From: gamepad_coder

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 How to check type of a custom Class? - Archive - Godot Forum

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

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.

MintSoda | 2021-01-24 01:36

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

runapp | 2021-02-03 17:20

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

func get_class():
    return "CustomClass"

a.get_class() == self.get_class()

MintSoda | 2021-03-05 22:50

:bust_in_silhouette: Reply From: digitaldipak

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