|
|
|
|
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_name
s,
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