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

Update typed arrays documentation #79075

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions doc/classes/Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,30 @@
<param index="2" name="class_name" type="StringName" />
<param index="3" name="script" type="Variant" />
<description>
Creates a typed array from the [param base] array.
Creates a typed array from the [param base] array. All arguments are required.
- [param type] is the built-in type as a [enum Variant.Type] constant, for example [constant TYPE_INT].
- [param class_name] is the [b]native[/b] class name, for example [Node]. If [param type] is not [constant TYPE_OBJECT], must be an empty string.
- [param script] is the associated script. Must be a [Script] instance or [code]null[/code].
Examples:
[codeblock]
class_name MyNode
extends Node

class MyClass:
pass

func _ready():
var a = Array([], TYPE_INT, &amp;"", null) # Array[int]
var b = Array([], TYPE_OBJECT, &amp;"Node", null) # Array[Node]
var c = Array([], TYPE_OBJECT, &amp;"Node", MyNode) # Array[MyNode]
var d = Array([], TYPE_OBJECT, &amp;"RefCounted", MyClass) # Array[MyClass]
[/codeblock]
[b]Note:[/b] This constructor can be useful if you want to create a typed array on the fly, but you are not required to use it. In GDScript you can use a temporary variable with the static type you need and then pass it:
[codeblock]
func _ready():
var a: Array[int] = []
some_func(a)
[/codeblock]
</description>
</constructor>
<constructor name="Array">
Expand Down Expand Up @@ -317,19 +340,19 @@
<method name="get_typed_builtin" qualifiers="const">
<return type="int" />
<description>
Returns the [enum Variant.Type] constant for a typed array. If the [Array] is not typed, returns [constant TYPE_NIL].
Returns the built-in type of the typed array as a [enum Variant.Type] constant. If the array is not typed, returns [constant TYPE_NIL].
</description>
</method>
<method name="get_typed_class_name" qualifiers="const">
<return type="StringName" />
<description>
Returns a class name of a typed [Array] of type [constant TYPE_OBJECT].
Returns the [b]native[/b] class name of the typed array if the built-in type is [constant TYPE_OBJECT]. Otherwise, this method returns an empty string.
</description>
</method>
<method name="get_typed_script" qualifiers="const">
<return type="Variant" />
<description>
Returns the script associated with a typed array tied to a class name.
Returns the script associated with the typed array. This method returns a [Script] instance or [code]null[/code].
Copy link
Member Author

@dalexeev dalexeev Jul 5, 2023

Choose a reason for hiding this comment

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

Actually it returns Object#null, but I think it's a bug.

print(null) # <null>
print([].get_typed_script()) # <Object#null>

Copy link
Member Author

@dalexeev dalexeev Jul 6, 2023

Choose a reason for hiding this comment

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

Now I'm not sure if this is a bug, this is the behavior of all Ref<T> properties and method return values. Should we draw the reader's attention to this difference?

var sprite := Sprite2D.new()
print(sprite.texture) # <Object#null>
print(sprite.texture == null) # true
print(is_same(sprite.texture, null)) # false
print(typeof(sprite.texture)) # 24
print(typeof(null)) # 0
Suggested change
Returns the script associated with the typed array. This method returns a [Script] instance or [code]null[/code].
Returns the script associated with the typed array. This method returns a [Script] instance (can be invalid).

</description>
</method>
<method name="has" qualifiers="const">
Expand Down
Loading