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

Add documentation note on Object's boolean context #99091

Merged

Conversation

Mickeon
Copy link
Contributor

@Mickeon Mickeon commented Nov 11, 2024

Futuristic awesome version of #93896

Self-explanatory. This note is present in some other Variant types and here it's particularly worth explaining here due of the change in #93885

See:

@Mickeon Mickeon added this to the 4.4 milestone Nov 11, 2024
@Mickeon Mickeon requested a review from a team as a code owner November 11, 2024 21:24
@@ -22,6 +22,7 @@
Lastly, every object can also contain metadata (data about data). [method set_meta] can be useful to store information that the object itself does not depend on. To keep your code clean, making excessive use of metadata is discouraged.
[b]Note:[/b] Unlike references to a [RefCounted], references to an object stored in a variable can become invalid without being set to [code]null[/code]. To check if an object has been deleted, do [i]not[/i] compare it against [code]null[/code]. Instead, use [method @GlobalScope.is_instance_valid]. It's also recommended to inherit from [RefCounted] for classes storing data instead of [Object].
[b]Note:[/b] The [code]script[/code] is not exposed like most properties. To set or get an object's [Script] in code, use [method set_script] and [method get_script], respectively.
[b]Note:[/b] In a boolean context, an [Object] will evaluate to [code]false[/code] if it is equal to [code]null[/code] or it has been freed. Otherwise, an [Object] will always evaluate to [code]true[/code]. See also [method @GlobalScope.is_instance_valid].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[b]Note:[/b] In a boolean context, an [Object] will evaluate to [code]false[/code] if it is equal to [code]null[/code] or it has been freed. Otherwise, an [Object] will always evaluate to [code]true[/code]. See also [method @GlobalScope.is_instance_valid].
[b]Note:[/b] In a boolean context, an [Object] will evaluate to [code]false[/code] if it is equal to [code]null[/code] or it is an invalid object (e.g. it has been deleted from memory). Otherwise, an [Object] will always evaluate to [code]true[/code]. See also [method @GlobalScope.is_instance_valid].

Since there is Object#null and for symmetry with is_instance_valid() docs:

Returns [code]true[/code] if [param instance] is a valid Object (e.g. has not been deleted from memory).

Also, it's worth adding a note to the is_instance_valid() docs that since 4.4 this function is mostly optional in GDScript, you get the same result whether you explicitly or implicitly cast an object to bool.

Copy link
Contributor Author

@Mickeon Mickeon Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once more there's an ambiguous "e.g." here. Just for clarity, it means less "That would mean" and more "Here's one example of invalid object"? I assume the latter. Either way, I may come up with another way to word it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for clarity, it means less "That would mean" and more "Here's one example of invalid object"? I assume the latter.

Yes, "invalid object" = Object#null | Freed Object.

Copy link
Contributor

@Repiteo Repiteo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, pending @dalexeev's suggestion

EDIT: Actually nah, I'll just merge it now. We can worry about the wording in a followup, but this functionality being specified period takes priority

@Repiteo Repiteo merged commit 2a147f3 into godotengine:master Nov 27, 2024
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Nov 27, 2024

Thanks!

@Mickeon
Copy link
Contributor Author

Mickeon commented Nov 27, 2024

Sorry, I got caught up in other things!

@Mickeon Mickeon deleted the documentation-object-boolean-context-4.4 branch November 27, 2024 16:55
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.

3 participants