Add append_array() method to Array class

(cherry picked from commit 9f23a94b8a)
This commit is contained in:
Tomasz Chabora 2020-11-08 21:09:45 +01:00 committed by Rémi Verschelde
parent f39c71e32d
commit 98774000e2
No known key found for this signature in database
GPG key ID: C3336907360768E1
4 changed files with 24 additions and 0 deletions

View file

@ -117,6 +117,11 @@ void Array::push_back(const Variant &p_value) {
_p->array.push_back(p_value);
}
void Array::append_array(const Array &p_array) {
_p->array.append_array(p_array._p->array);
}
Error Array::resize(int p_new_size) {
return _p->array.resize(p_new_size);

View file

@ -64,6 +64,7 @@ public:
void push_back(const Variant &p_value);
_FORCE_INLINE_ void append(const Variant &p_value) { push_back(p_value); } //for python compatibility
void append_array(const Array &p_array);
Error resize(int p_new_size);
void insert(int p_pos, const Variant &p_value);

View file

@ -534,6 +534,7 @@ struct _VariantCall {
VCALL_LOCALMEM0R(Array, pop_back);
VCALL_LOCALMEM0R(Array, pop_front);
VCALL_LOCALMEM1(Array, append);
VCALL_LOCALMEM1(Array, append_array);
VCALL_LOCALMEM1(Array, resize);
VCALL_LOCALMEM2(Array, insert);
VCALL_LOCALMEM1(Array, remove);
@ -1799,6 +1800,7 @@ void register_variant_methods() {
ADDFUNC1NC(ARRAY, NIL, Array, push_back, NIL, "value", varray());
ADDFUNC1NC(ARRAY, NIL, Array, push_front, NIL, "value", varray());
ADDFUNC1NC(ARRAY, NIL, Array, append, NIL, "value", varray());
ADDFUNC1NC(ARRAY, NIL, Array, append_array, ARRAY, "array", varray());
ADDFUNC1NC(ARRAY, NIL, Array, resize, INT, "size", varray());
ADDFUNC2NC(ARRAY, NIL, Array, insert, INT, "position", NIL, "value", varray());
ADDFUNC1NC(ARRAY, NIL, Array, remove, INT, "position", varray());

View file

@ -20,6 +20,7 @@
var array2 = [3, "Four"]
print(array1 + array2) # ["One", 2, 3, "Four"]
[/codeblock]
Note that concatenating with [code]+=[/code] operator will create a new array. If you want to append another array to an existing array, [method append_array] is more efficient.
[b]Note:[/b] Arrays are always passed by reference. To get a copy of an array which can be modified independently of the original array, use [method duplicate].
</description>
<tutorials>
@ -95,6 +96,21 @@
Appends an element at the end of the array (alias of [method push_back]).
</description>
</method>
<method name="append_array">
<return type="void">
</return>
<argument index="0" name="array" type="Array">
</argument>
<description>
Appends another array at the end of this array.
[codeblock]
var array1 = [1, 2, 3]
var array2 = [4, 5, 6]
array1.append_array(array2)
print(array1) # Prints [1, 2, 3, 4, 5, 6].
[/codeblock]
</description>
</method>
<method name="back">
<return type="Variant">
</return>