Skip to content
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

Merged
merged 1 commit into from
Feb 22, 2023

Conversation

dalexeev
Copy link
Member

This function was added in #73489.

CC @vonagam

@dalexeev dalexeev requested a review from a team as a code owner February 22, 2023 13:16
@dalexeev dalexeev force-pushed the doc-gds-is-instance-of branch from 67de86c to 059cb07 Compare February 22, 2023 13:25
@@ -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:
Copy link
Member

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.

Copy link
Member Author

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.

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;
}

Copy link
Member

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.

Copy link
Member Author

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.

@akien-mga akien-mga merged commit 999bb91 into godotengine:master Feb 22, 2023
@akien-mga
Copy link
Member

Thanks!

@dalexeev dalexeev deleted the doc-gds-is-instance-of branch February 22, 2023 15:57
- 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.
Copy link
Member Author

@dalexeev dalexeev Feb 22, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants