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

Array.resize() doesn't mention typed arrays #9005

Closed
thiagola92 opened this issue Feb 22, 2024 · 4 comments · Fixed by godotengine/godot#69451
Closed

Array.resize() doesn't mention typed arrays #9005

thiagola92 opened this issue Feb 22, 2024 · 4 comments · Fixed by godotengine/godot#69451
Labels
area:class reference Issues and PRs about the class reference, which should be addressed on the Godot engine repository enhancement

Comments

@thiagola92
Copy link
Contributor

Your Godot version: v4.2.1.stable.official [b09f793f5]

Issue description:
It's silly to report this but this is incorrect when the array has type:

If the array size is smaller, elements are cleared, if bigger, new elements are null.

They receive the initialization value for their type.

var a = []
var b: Array = []
var c: Array[bool] = []
var d: Array[int] = []
var e: Array[float] = []
var f: Array[String] = []
var g: Array[Array] = []
var h: Array[Dictionary] = []

a.resize(1)
b.resize(1)
c.resize(1)
d.resize(1)
e.resize(1)
f.resize(1)
g.resize(1)
h.resize(1)

printt("a", a)
printt("b", b)
printt("c", c)
printt("d", d)
printt("e", e)
printt("f", f)
printt("g", g)
printt("h", h)

Result:

a	[<null>]
b	[<null>]
c	[false]
d	[0]
e	[0]
f	[""]
g	[[]]
h	[{  }]

URL to the documentation page:
https://docs.godotengine.org/en/latest/classes/class_array.html#class-array-method-resize

@Mickeon
Copy link
Contributor

Mickeon commented Feb 22, 2024

I was about to say that godotengine/godot#69451 would've solved it but even there it mentions null so I can keep this one in mind soon.

Technically saying null is not wrong internally, since if any null Variant is forced to convert, the result is what you see in the code sample above. Still, worth specifying.

@Mickeon Mickeon added the area:class reference Issues and PRs about the class reference, which should be addressed on the Godot engine repository label Feb 22, 2024
@thiagola92
Copy link
Contributor Author

any null Variant is forced to convert, the result is what you see in the code sample above. Still, worth specifying.

Hmmm interesting... I was going to guess that was calling something like type_convert() but it seems not.

var c: Array[bool] = [type_convert(null, TYPE_BOOL)]
var d: Array[int] = [type_convert(null, TYPE_INT)]
var e: Array[float] = [type_convert(null, TYPE_FLOAT)]
var f: Array[String] = [type_convert(null, TYPE_STRING)]
var g: Array[Array] = [type_convert(null, TYPE_ARRAY)]
var h: Array[Dictionary] = [type_convert(null, TYPE_DICTIONARY)]

printt("c", c)
printt("d", d)
printt("e", e)
printt("f", f)
printt("g", g)
printt("h", h)

Because null is converted to "<null>" instead of empty string like resize() does.

c	[false]
d	[0]
e	[0]
f	["<null>"]
g	[[]]
h	[{  }]

@Mickeon
Copy link
Contributor

Mickeon commented Feb 22, 2024

Well, actually, it may be more appropriate to say resized fills the array with the "default" for the given type. In other words it's the same value as an empty constructor for the type (except Object).

All of those type_convert are somewhat failing and returning the default as well, except the String conversion which does exist.

@thiagola92
Copy link
Contributor Author

I should have read type_convert 😆
Well, everything make sense, thank you for the attention!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:class reference Issues and PRs about the class reference, which should be addressed on the Godot engine repository enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants