ソースを参照

Add append_array() method to Array class

(cherry picked from commit 9f23a94b8adbdae83648080a73f9201c03ba4e0d)
Tomasz Chabora 4 年 前
コミット
98774000e2
4 ファイル変更24 行追加0 行削除
  1. 5 0
      core/array.cpp
  2. 1 0
      core/array.h
  3. 2 0
      core/variant_call.cpp
  4. 16 0
      doc/classes/Array.xml

+ 5 - 0
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);

+ 1 - 0
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);

+ 2 - 0
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());

+ 16 - 0
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].
 	</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>