1

I'm trying to check if an object adheres to certain restrictions by checking its type against a given check_type variable. This variable is set dynamically, so I cannot check against a fixed class name.

var check_type = get_type_restriction()
if thing_to_check is check_type:
    do_something()

I tried several things (Object type hint for check_type, Script type hint, no type hint) but get the error: check_type is a variable but does not contain a type, or: local variable check_type cannot be used as a type.

3
  • For dynamic type checks, use is_class("ClassName"). You cannot use a variable with is in GDScript, is only works with static type hints. Commented Sep 29 at 13:40
  • @Kamran ok, so to do this dynamically I save the class name as a string? Thanks! Commented Sep 29 at 13:47
  • Note that the answer only works for Godot classes, not for custom classes (with class_name X). For this, it's best to use is_instance_of. Commented Sep 30 at 15:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.