-
-
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: Fix typed arrays #69248
GDScript: Fix typed arrays #69248
Conversation
51aa2ee
to
e3d840e
Compare
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
1776e7d
to
857661b
Compare
Fixed another thing about arrays (not connected to literals or constants) - typed assigns. Right now if element types are somewhat compatible a destination array just references a source array losing its own type in a process. class A: pass
class B extends A: pass
func _ready():
var b: Array[ B ] = [ B.new() ]
var a: Array[ A ] = b # before: a references b, becomes Array[ B ], after: a copies b contents
a.push_back( A.new() )
a.push_back( B.new() )
print( b.size() ) # before: 2, now: 1
print( a.size() ) # before: 2, now: 3
check( a ) # before: error, now: works
func check( a: Array[ A ] ) -> void: print( 'check' ) To make it correct array |
You need to squash the commits as required by our pipeline (https://docs.godotengine.org/en/latest/community/contributing/pr_workflow.html#modifying-a-pull-request). |
Oh, I was doing that initially, but then it grew and I assume became harder to review in a single commit. Also I assume that third commit might be dropped (about flatness/deepness of constants). |
857661b
to
19044d7
Compare
This comment was marked as outdated.
This comment was marked as outdated.
19044d7
to
c42456b
Compare
This comment was marked as outdated.
This comment was marked as outdated.
c42456b
to
373cbfb
Compare
After I made |
373cbfb
to
92787b6
Compare
Fixed another major thing about array assigns. var first: Array[ A ] = []
var second: Array[ A ] = first
var value: Variant = A.new()
second = [ value ]
print( first.size() ) # before: 1, after: 0
print( second.size() ) # 1 Currently |
92787b6
to
c610f52
Compare
This comment was marked as outdated.
This comment was marked as outdated.
f949908
to
d58b7f8
Compare
6c5ca93
to
ad50cc1
Compare
This comment was marked as outdated.
This comment was marked as outdated.
ad50cc1
to
9b15002
Compare
|
0a292a3
to
7416c19
Compare
Note that C#'s In case of "our method" it might end up just assigning the internal array which is copy-on-write and hence actual copying can happen later. So |
I don't how it is misleading. It changes contents to match those of source array. That's it. copy-on-write is just an internal optimization/implementation, has nothing to do with behavior. I see that Python has |
62e16fc
to
245c7cc
Compare
I added such code to if (value.get_type() == Variant::ARRAY) {
Array set_array = value;
bool is_get_valid = false;
Variant get_value = node->get(snames[nprops[j].name], &is_get_valid);
if (is_get_valid && get_value.get_type() == Variant::ARRAY) {
Array get_array = get_value;
if (!set_array.is_same_typed(get_array)) {
value = Array(set_array, get_array.get_typed_builtin(), get_array.get_typed_class_name(), get_array.get_typed_script());
}
}
} So if an array being set but it is possible to get a default one and the element types are different then try to convert the incoming array to type of currently present one. Not super ideal - proper is to use |
245c7cc
to
5909f9f
Compare
Added the same logic to |
Thanks! |
Congratulations on this huge PR! I know it was a loooooooooot of work with a looooooooot of subtleties, but so very important! Very very excited this got in! :) :) :) |
This is complete rework how typed arrays do function. In terms of interactions now typed arrays act like packed arrays do.
No more implicit conversions between arrays on assigns.
Array
(without a parameter) for typed arrays is whatVariant
is for other types. It is possible to assign typed array to assignable (variable/parameter/constant) with type of basic array and it is possible to do the opposite, but later fails if types do not match.Explicit inference no longer tries to guess what element type an array has, it will be typed as basic one.
Typed arrays in constants now work properly.
There is no implicit conversions between arrays, but there is for array literals. In all examples above literal on right side is a usual array literal, but it gets converted during analysis to be of proper type and fails if it is impossible to do. It applies to variable/parameters/constants initializers, assignments, returns, arguments and casts:
typed_assign
now does only one thing - replaces contents of an array with contents from another with conversions if needed. It is also renamed to simpleassign
.Fixed
resize
not usingresize_zeroed
and not initializing new elements in typed arrays. This was leading to memory corruptions and crashes.In
VariantInterals::initialize
add missing initailization forProjection
andColor
(alpha needs to be set at 1).Added serialization/deserialization of typed arrays. Though it does not handle custom classes at the moment.
Fixed undo/redo for packed arrays, for some reason they were excluded from that before.
Changed editors for arrays/dictionaries a little - no need to do recursively duplicate them on each rerender, do it only when value is about to be modified.
Fixed default values for arrays/dictionaries not showing in editor.
Fixed
GDScriptInstance::set
not converting values when setters involved.Related issues:
Fixes #53847 (about parameters).
Fixes #54643 (about parameters).
Fixes #55916 (about constants).
Fixes #56389 (about documentation).
Fixes #58285 (about editor).
Fixes #58636 (about parameters).
Fixes #62753 (about editor).
Fixes #63502 (about default values).
Fixes #63570 (about resize).
Fixes #67653 (about editor).
Fixes #67889 (about literals).
Fixes #68978 (about constants).
Fixes #69198 (about constants).
Fixes #69839 (about assigns).
Fixes #70021 (about constants).
Fixes #70235 (about resize).
Fixes #71607 (about editor).
Fixes #71653 (about parameters).
Fixes #71756 (about enums).
Fixes #71815 (about editor).
Fixes #72349 (about parameters).