diff options
author | Kazuo256 <kazuo256@gmail.com> | 2016-11-18 18:30:16 -0200 |
---|---|---|
committer | Kazuo256 <kazuo256@gmail.com> | 2016-11-18 18:45:51 -0200 |
commit | bf4fda64fd403d589278919cff01c3207164207e (patch) | |
tree | bed9a1d2244b31ec27a40fa18cda6c6357608379 | |
parent | 959683c3d8f7aaa7469d5758c7698e0da108b406 (diff) |
Add Array.front() and Array.back()
-rw-r--r-- | core/array.cpp | 10 | ||||
-rw-r--r-- | core/array.h | 3 | ||||
-rw-r--r-- | core/variant_call.cpp | 4 | ||||
-rw-r--r-- | doc/base/classes.xml | 14 |
4 files changed, 31 insertions, 0 deletions
diff --git a/core/array.cpp b/core/array.cpp index 683a43e3d0..f7ca67b6a3 100644 --- a/core/array.cpp +++ b/core/array.cpp @@ -150,6 +150,16 @@ void Array::erase(const Variant& p_value) { _p->array.erase(p_value); } +Variant Array::front() const { + ERR_FAIL_COND_V(_p->array.size() == 0, Variant()); + return operator[](0); +} + +Variant Array::back() const { + ERR_FAIL_COND_V(_p->array.size() == 0, Variant()); + return operator[](_p->array.size() - 1); +} + int Array::find(const Variant& p_value, int p_from) const { return _p->array.find(p_value, p_from); diff --git a/core/array.h b/core/array.h index eb79b0cf33..fb417b6ec0 100644 --- a/core/array.h +++ b/core/array.h @@ -67,6 +67,9 @@ public: void insert(int p_pos, const Variant& p_value); void remove(int p_pos); + Variant front() const; + Variant back() const; + void sort(); void sort_custom(Object *p_obj,const StringName& p_function); void invert(); diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 9b6fa27cf4..0379ae019b 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -472,6 +472,8 @@ static void _call_##m_type##_##m_method(Variant& r_ret,Variant& p_self,const Var VCALL_LOCALMEM1(Array,resize); VCALL_LOCALMEM2(Array,insert); VCALL_LOCALMEM1(Array,remove); + VCALL_LOCALMEM0R(Array,front); + VCALL_LOCALMEM0R(Array,back); VCALL_LOCALMEM2R(Array,find); VCALL_LOCALMEM2R(Array,rfind); VCALL_LOCALMEM1R(Array,find_last); @@ -1576,6 +1578,8 @@ _VariantCall::addfunc(Variant::m_vtype,Variant::m_ret,_SCS(#m_method),VCALL(m_cl ADDFUNC2(ARRAY,NIL,Array,insert,INT,"pos",NIL,"value",varray()); ADDFUNC1(ARRAY,NIL,Array,remove,INT,"pos",varray()); ADDFUNC1(ARRAY,NIL,Array,erase,NIL,"value",varray()); + ADDFUNC0(ARRAY,NIL,Array,front,varray()); + ADDFUNC0(ARRAY,NIL,Array,back,varray()); ADDFUNC2(ARRAY,INT,Array,find,NIL,"what",INT,"from",varray(0)); ADDFUNC2(ARRAY,INT,Array,rfind,NIL,"what",INT,"from",varray(-1)); ADDFUNC1(ARRAY,INT,Array,find_last,NIL,"value",varray()); diff --git a/doc/base/classes.xml b/doc/base/classes.xml index 246f8cdac6..167fbb32cb 100644 --- a/doc/base/classes.xml +++ b/doc/base/classes.xml @@ -4656,6 +4656,20 @@ Remove the first occurrence of a value from the array. </description> </method> + <method name="front"> + <return type="Variant"> + </return> + <description> + Returns the first element of the array if the array is not empty (size>0). + </description> + </method> + <method name="back"> + <return type="Variant"> + </return> + <description> + Returns the last element of the array if the array is not empty (size>0). + </description> + </method> <method name="find"> <return type="int"> </return> |