Search code examples
godotgdscript

Godot: How to recognize the type of a built-in type?


In GDScript the is keyword can be used to check whether a value is an instance of a type:

if (input is SomeClass):
    # this works fine

But you can't do that for primitive "built-in" types like strings:

if (input is String):
   # this won't compile 

That gives me a "Parser Error: misplaced expression, misplaced: Built-In Type"

So how do you check if an input is a string?


Solution

  • Found it!

    You can't use is for primitives, but instead there's a typeof function:

    if typeof(input) == TYPE_STRING
    

    The value there is a TYPE enum in @GlobalScope.

    If your value o is an instance of a class, typeof(o) will return TYPE_OBJECT.