0 votes

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).

in Engine by (66 points)
edited by

1 Answer

0 votes
by (20,475 points)

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?

"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():
https://docs.godotengine.org/en/latest/classes/class_@gdscript.html#class-gdscript-method-typeof

Using this you get a type enum, which is listed here:
https://docs.godotengine.org/en/latest/classes/class_@globalscope.html#class-globalscope

So, for example:

if typeof(n) == TYPE_STRING:
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.