Godot:如何识别内置类型的类型?

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

在 GDScript 中,is 关键字可用于检查值是否是类型的实例:

if (input is SomeClass):
    # this works fine

但是你不能对像字符串这样的原始 "built-in" 类型这样做:

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

这给了我一个 "Parser Error: misplaced expression, misplaced: Built-In Type"

那么如何检查输入是否为字符串?

找到了!

您不能将 is 用于原语,而是有一个 typeof 函数:

if typeof(input) == TYPE_STRING

@GlobalScope 中有一个 TYPE 枚举的值。

如果您的值 o 是 class 的一个实例,typeof(o) 将 return TYPE_OBJECT