How to do an 'is type of' check when you have a string name instead of a literal type name?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By goshot

How to do this:

if n is Control:

When you have a string that is the name of the type?

type = "Control" # Comes from somewhere, like an exported var set in editor
if n is type_string:

The latter causes Parser Error: Invalid ‘is’ test: right operand is not a type (not a native type nor a script).

:bust_in_silhouette: Reply From: kidscancode

The type of a string is String not str.

I didn’t ask my question very well, so I modified it a bit. My question is how to use a string with “is” instead of a literal type name - perhaps by turning it into a type?

goshot | 2019-07-18 16:12

“turning it into a type” doesn’t make sense, but I think I see what you are looking for.

GDScript has a method called typeof():
@GDScript — Godot Engine (latest) documentation in English

Using this you get a type enum, which is listed here:
@GlobalScope — Godot Engine (latest) documentation in English

So, for example:

if typeof(n) == TYPE_STRING:

kidscancode | 2019-07-18 16:22