-
-
Notifications
You must be signed in to change notification settings - Fork 22.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document @GDScript.is_instance_of
method
#73761
Conversation
67de86c
to
059cb07
Compare
@@ -126,6 +126,20 @@ | |||
<param index="0" name="value" type="Variant" /> | |||
<param index="1" name="type" type="Variant" /> | |||
<description> | |||
Returns [code]true[/code] if [param value] is an instance of [param type]. The [param type] value must be one of the following: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the recent bug reports we got, it seems like users use it this way:
var a = 5
var b = 42
print(is_instance_of(a, b))
Provided this does work, I don't think it's covered by the list below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't work because 42 >= Variant::VARIANT_MAX
.
godot/modules/gdscript/gdscript_utility_functions.cpp
Lines 530 to 541 in 5f3dee5
if (p_args[1]->get_type() == Variant::INT) { | |
int builtin_type = *p_args[1]; | |
if (builtin_type < 0 || builtin_type >= Variant::VARIANT_MAX) { | |
*r_ret = RTR("Invalid type argument for is_instance_of(), use TYPE_* constants for built-in types."); | |
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; | |
r_error.argument = 1; | |
r_error.expected = Variant::NIL; | |
return; | |
} | |
*r_ret = p_args[0]->get_type() == builtin_type; | |
return; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of #73663, but I guess this was just a bogus example? I thought they were trying to use this to confirm that both variants are ints.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the issue, I first thought the user was trying to use is
like in Python. But maybe it really was just a bad example and the problem was the confusing error message.
059cb07
to
a98a0f5
Compare
Thanks! |
- A constant from the [enum Variant.Type] enumeration, for example [constant TYPE_INT]. | ||
- An [Object]-derived class which exists in [ClassDB], for example [Node]. | ||
- A [Script] (you can use any class, including inner one). | ||
Unlike the right operand of the [code]is[/code] operator, [param type] can be a non-constant value. The [code]is[/code] operator supports more features (such as typed arrays) and is more performant. Use the operator instead of this method if you do not need dynamic type checking. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I shouldn't have written about performance, especially without tests. This is too implementation dependent. But I'm surprised that is_instance_of()
is faster, if my tests are correct.
This function was added in #73489.
CC @vonagam