-
-
Notifications
You must be signed in to change notification settings - Fork 21.1k
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 fill method to Arrays and PackedArrays #46476
Conversation
I'm not sure about the extra logic for duplicating container types. Things like |
While I agree it's a bit counter intuitive to duplicate, I though it would be a little weird to have an array where every position is the same object. However, I can make it optional or simply remove the duplication, If requested. |
Due to the reasons pointed by @Xrayez, I've removed the duplication on container types. This should make the method more consistent and avoid future bugs related to |
I suggest refactoring the code this way: // vector.h
template <class T>
void Vector<T>::fill(T p_elem) {
T *p = ptrw();
for (int i = 0; i < size(); i++) {
p[i] = p_elem;
}
} // array.cpp
void Array::fill(const Variant &p_value) {
ERR_FAIL_COND(!_p->typed.validate(p_value, "fill"));
_p->array.fill(p_value);
}
|
Thanks for the feedback, @Xrayez. I've updated the code and the documentation with your chances. I've also added the documentation for |
doc/classes/Array.xml
Outdated
<argument index="0" name="value" type="Variant"> | ||
</argument> | ||
<description> | ||
Inserts a new element at every position of the array. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like this was adapted from the insert
description, that doesn't sound clear to me. I'd suggest instead:
Inserts a new element at every position of the array. | |
Initializes all elements in the array with the given value. This can typically be used together with [method resize] to create an array with a given size and initialized elements: | |
[codeblocks] | |
[gdscript] | |
var array = [] | |
array.resize(10) | |
array.fill(0) # Initialize the 10 elements to 0. | |
[/gdscript] | |
[csharp] | |
var array = new Godot.Collections.Array{}; | |
array.Resize(10); | |
array.Fill(0); // Initialize the 10 elements to 0. | |
[/csharp] | |
[/codeblocks] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code example might not need to be added to all Packed*Array
equivalent descriptions though, might be a bit redundant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, it was adapted from insert
. I will update the descriptions ASAP, but I might change the word "initialize" to something else. I don't know if it's just me, but it gives me the ideia of something that you do when creating the array, but you can call array.fill() whenever (you could use it to "reset" an array or something).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it could be "Sets all elements in the array to the given value." or "Assigns the given value to all elements in the array."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the documentation. Thanks for the help and suggestions!
doc/classes/PackedColorArray.xml
Outdated
<argument index="0" name="value" type="Color"> | ||
</argument> | ||
<description> | ||
Assigns the given value to all elements in the array. This can typically be used together with [method resize] to create an array with a given size and initialized elements |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just missing a dot at the end of the sentence for all Packed*Array
descriptions, otherwise it's all good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah, my fault for copy pasting and not double checking. Thanks for the heads up, it's should be fixed now.
Thanks! |
Can this get backported to 3.x? |
Yes, but this PR needs to be manually redone for |
Closes godotengine/godot-proposals#2181
Based on the discussion on #46173
Adds a
fill(value)
method for Arrays and PackedArrays that allows filling the entire array with a single value.Example usage:
I would also like to thank @anberino. He ran all tests for me (since my gpu doesn't support vulkan) and this PR wouldnt be possible without his help.