From 98774000e2a075eec39ab98ba324d6e302e40786 Mon Sep 17 00:00:00 2001 From: Tomasz Chabora Date: Sun, 8 Nov 2020 21:09:45 +0100 Subject: [PATCH] Add append_array() method to Array class (cherry picked from commit 9f23a94b8adbdae83648080a73f9201c03ba4e0d) --- core/array.cpp | 5 +++++ core/array.h | 1 + core/variant_call.cpp | 2 ++ doc/classes/Array.xml | 16 ++++++++++++++++ 4 files changed, 24 insertions(+) diff --git a/core/array.cpp b/core/array.cpp index e488034c6aa..14f75c8ea4c 100644 --- a/core/array.cpp +++ b/core/array.cpp @@ -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); diff --git a/core/array.h b/core/array.h index 63454420e21..910b231e7b4 100644 --- a/core/array.h +++ b/core/array.h @@ -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); diff --git a/core/variant_call.cpp b/core/variant_call.cpp index d3ff10b1175..0c811342332 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -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()); diff --git a/doc/classes/Array.xml b/doc/classes/Array.xml index d0ea8d483f9..e8cf7f674f6 100644 --- a/doc/classes/Array.xml +++ b/doc/classes/Array.xml @@ -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]. @@ -95,6 +96,21 @@ Appends an element at the end of the array (alias of [method push_back]). + + + + + + + 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] + +