-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
GDScript typed arrays #46830
GDScript typed arrays #46830
Conversation
I would argue that var var_list : Array = []
var int_list : Array[int] = []
# versus
var var_list : Array = []
var int_list : [int] = [] |
@pycbouh that's actually a good point |
I'll make the change and also found another issue, so converting to draft for now. |
d942079
to
8d679b0
Compare
Should be okay to merge. There are still some issues with some typed arrays, like I mentioned but I'm discussing with reduz a solution and could be done in another PR. |
@vnen, I was doing some testing using 4.0 branch and found that accessing a Dictionary has issues.
|
@vonflyhighace2 This was already reported here: #44241 |
@hilfazer I think that issues vnen mentioned are precisely about that. Values are not properly validated, and it's the same internally (i.e. it's not a bug introduced in this PR) |
Possibly, but i'd rather mention known problems than not mention problems people are not aware of. There's also this:
It doesn't allow me to append an int. Type got inferred as |
whats the diffrence between a typed array and a packed array ? |
There are still some issues in the core Typed Array, I'm working on fixing those. |
A packed array is a contiguous memory location with every element of the same type. A typed array is still an array of Variants, but there are extra guarantees that every element will have the same type. So packed arrays are a discrete list (only those shown are available) and typed arrays can have any valid type. |
6d8a083
to
0e0bf4c
Compare
@hilfazer those issues should be fixed now. |
Isn't this the same as |
The second issue is fixed indeed but the first one remains (both for local and script variables). I've also did some testing for 2d typed arrays:
Inner array is a regular array during runtime, not Array[int]. |
@MaaaxiKing There are no PoolArrays in master, they are somewhat replaced by PackedArrays. As to the difference, look at the comment above: |
@hilfazer nested typed arrays are not supported. Edit: I'll add an error message for this case. |
@vnen does this also apply to dictionary and how keys are validated? |
@vonflyhighace2 this is only for arrays. |
0e0bf4c
to
961c385
Compare
Updated to add an error message in nested arrays and when adding the element type to anything that isn't array (since those aren't supported for now). Should have fixed the reported issues too. |
961c385
to
3015fd5
Compare
The issues i mentioned are solved! But i found a new one (:
It returns a normal array. |
The array should just assimilate the type of the other one since assignment in this case means a change in the reference. This also adds a `typed_assign` function for the cases where type validation is wanted.
This ensure that typed arrays are properly checked when setting an element. Moved the macro to a straight declaration since the macro was only used for Array and it now is quite specific to the Array class.
3015fd5
to
293be0f
Compare
@hilfazer ah, I forgot about this case, should be fixed now. Thanks for testing! |
That last issue i posted is now dealt with. But i decided to give it some serious testing and found some more:
|
I have this crash when running https://github.com/qarmin/Qarminer/archive/refs/heads/master.zip with address sanitizer
|
- Use `Array[type]` for type-hints. e.g.: `var array: Array[int] = [1, 2, 3]` - Array literals are typed if their storage is typed (variable asssignment of as argument in function all). Otherwise they are untyped.
293be0f
to
85e316a
Compare
The crash should be solved now. I believe this is good to merge. The error with the return value is actually deeper than this so I would prefer to fix it in a different PR. |
Thanks! |
Probably since this commit I have this message when running editor with undefined sanitizer:
|
@qarmin ah, nice catch, I've made a copy paste mistake there. |
Are these supposed to support Object types? All the examples I've seen have been with int/float/String/NodePath and those seem to work fine, but if I try to create say an |
There's a bit of a snag with the typed arrays implementation in Godot 4.1.2rc (haven't tried earlier version of Godot 4) From my experience, this is an example: I have set up a scene for my settings menu. To allow myself to set the Toggle buttons state properly when the menu is open after the settings were loaded from a saved file, I access the Button Groups and, in each case, tell Godot which button should be pressed. To access each Button Group, if I @export every Button Groups separately (not in an array) as variables of type ButtonGroup, then set their references in the inspector I was able to connect and access those Button Groups just fine even if those Button Group were "Local to Scene" toggled ON. This would look like this:
...and so on. It works and I was able to use something like this:
to set, for example, which of the buttons in the Language Button Group would be pressed. Now, if I was to use an array instead for getting all the button groups like this:
Then I setup the array in the editor similarly to when I set up the individual variables previously.
If the check box "Local to Scene" of the button group is toggled ON, the SceneButtonGroups[0] of my line above will refer, once the scene is loaded, to an new Object ID that is different from the original Object ID which is the Button Group located in the file (that is a normal behavior). But when I look at the content of that newly generated (by the scene) Button Group, it has 0 members. I went further into thing and looked at the Button Group from which the buttons are members of (in remote view). If I look up the members of the Button Group Object ID 9223372079083029868, I do get a reference to the right 2 buttons as members. At the moment, the only way of making such this problem doesn't appear is to make it so that any references in an array of certain types (if applicable) is set to Global and not Scene Local within their resources parameters. |
@dongohu you should open an issue about your problem. It is hard to keep track of comments in closed PRs. |
This add typed arrays as valid GDScript.
Syntax
Borrowing from Python since @pycbouh convinced me. It makes it easier to change from a typed array to a generic one and gets more consistent. It open margins for other collection types as well.
Inference is also possible:
There's not an exact way to create a typed array literal. This is inferred by the context: if assigned to a typed variable or passed as a function argument to a typed parameter, then the array is typed (assuming all elements if of the same type). If this is deemed not enough we may consider adding one later, but for now I don't see a need.
Caveats
This relies on the typed array implementation from #38063. This shows an error in the console but it doesn't break the execution of the script.
I've also had some issues with the API likeNode.get_children()
which in theory returns a typed array but it doesn't have any validation if you try to change it (this is separate from this PR though and should be fixed on the typed array implementation).Edit: I've included here the needed changes in Array. You still don't get breakpoints when some errors happen though, as they are deep into the Array implementation and GDScript can't know about those.
Incidentally, this fixes issues with uninitialized values:
Fix #41631
Fix #44442
Fix #44793