diff options
author | est31 <MTest31@outlook.com> | 2016-02-24 02:21:56 +0100 |
---|---|---|
committer | est31 <MTest31@outlook.com> | 2016-02-28 22:47:48 +0100 |
commit | f81153eb6997c16318a5a7df5e982f013ebad37b (patch) | |
tree | 65360d141dfad5e1602f65744caa65c67e755bcb | |
parent | bc87ce62365b71ba5da5afffd8ddf90c6ca4af0a (diff) |
Add xrange builtin function
Also update classes.xml in order to document xrange
-rw-r--r-- | doc/base/classes.xml | 43 | ||||
-rw-r--r-- | modules/gdscript/gd_functions.cpp | 83 | ||||
-rw-r--r-- | modules/gdscript/gd_functions.h | 1 |
3 files changed, 127 insertions, 0 deletions
diff --git a/doc/base/classes.xml b/doc/base/classes.xml index 258feadf20..12948d4489 100644 --- a/doc/base/classes.xml +++ b/doc/base/classes.xml @@ -556,6 +556,15 @@ Return an array with the given range. Range can be 1 argument N (0 to N-1), two arguments (initial, final-1) or three arguments (initial,final-1,increment). </description> </method> + <method name="xrange"> + <return type="Object"> + </return> + <argument index="0" name="..." type="Variant"> + </argument> + <description> + Return an iterator over the given range. Range can be 1 argument N (0 to N-1), two arguments (initial, final-1) or three arguments (initial,final-1,increment). + </description> + </method> <method name="load"> <return type="Resource"> </return> @@ -27597,6 +27606,40 @@ This method controls whether the position between two cached points is interpola <constants> </constants> </class> +<class name="RangeIterator" inherits="Reference" category="Core"> + <brief_description> + </brief_description> + <description> + </description> + <methods> + <method name="is_finished"> + <return type="bool"> + </return> + <description> + </description> + </method> + <method name="to_array"> + <return type="Array"> + </return> + <description> + </description> + </method> + <method name="set_range"> + <return type="Object"> + </return> + <argument index="0" name="arg1" type="var"> + </argument> + <argument index="1" name="arg2" type="var" default="NULL"> + </argument> + <argument index="2" name="arg3" type="var" default="NULL"> + </argument> + <description> + </description> + </method> + </methods> + <constants> + </constants> +</class> <class name="RawArray" category="Built-In Types"> <brief_description> Raw byte array. diff --git a/modules/gdscript/gd_functions.cpp b/modules/gdscript/gd_functions.cpp index 9b7d8eeac4..e5689d8865 100644 --- a/modules/gdscript/gd_functions.cpp +++ b/modules/gdscript/gd_functions.cpp @@ -32,6 +32,7 @@ #include "reference.h" #include "gd_script.h" #include "func_ref.h" +#include "range_iterator.h" #include "os/os.h" #include "variant_parser.h" #include "io/marshalls.h" @@ -98,6 +99,7 @@ const char *GDFunctions::get_func_name(Function p_func) { "var2bytes", "bytes2var", "range", + "xrange", "load", "inst2dict", "dict2inst", @@ -816,6 +818,81 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va } } break; + case GEN_XRANGE: { + + switch(p_arg_count) { + case 0: { + r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument=1; + } break; + case 1: { + + VALIDATE_ARG_NUM(0); + + int count=*p_args[0]; + + Ref<RangeIterator> itr = Ref<RangeIterator>( memnew(RangeIterator) ); + if (!*itr) { + ERR_EXPLAIN("Couldn't allocate iterator!"); + r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD; + ERR_FAIL(); + } + (*itr)->set_range(count); + r_ret=Variant(itr); + return; + } break; + case 2: { + + VALIDATE_ARG_NUM(0); + VALIDATE_ARG_NUM(1); + + int from=*p_args[0]; + int to=*p_args[1]; + + Ref<RangeIterator> itr = Ref<RangeIterator>( memnew(RangeIterator) ); + if (!*itr) { + ERR_EXPLAIN("Couldn't allocate iterator!"); + r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD; + ERR_FAIL(); + } + (*itr)->set_range(from, to); + r_ret=Variant(itr); + return; + } break; + case 3: { + + VALIDATE_ARG_NUM(0); + VALIDATE_ARG_NUM(1); + VALIDATE_ARG_NUM(2); + + int from=*p_args[0]; + int to=*p_args[1]; + int incr=*p_args[2]; + + if (incr==0) { + ERR_EXPLAIN("step argument is zero!"); + r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD; + ERR_FAIL(); + } + + Ref<RangeIterator> itr = Ref<RangeIterator>( memnew(RangeIterator) ); + if (!*itr) { + ERR_EXPLAIN("Couldn't allocate iterator!"); + r_error.error=Variant::CallError::CALL_ERROR_INVALID_METHOD; + ERR_FAIL(); + } + (*itr)->set_range(from, to, incr); + r_ret=Variant(itr); + return; + } break; + default: { + + r_error.error=Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; + r_error.argument=3; + } break; + } + + } break; case RESOURCE_LOAD: { VALIDATE_ARG_COUNT(1); if (p_args[0]->get_type()!=Variant::STRING) { @@ -1433,6 +1510,12 @@ MethodInfo GDFunctions::get_info(Function p_func) { mi.return_val.type=Variant::ARRAY; return mi; } break; + case GEN_XRANGE: { + + MethodInfo mi("xrange",PropertyInfo(Variant::NIL,"...")); + mi.return_val.type=Variant::OBJECT; + return mi; + } break; case RESOURCE_LOAD: { MethodInfo mi("load",PropertyInfo(Variant::STRING,"path")); diff --git a/modules/gdscript/gd_functions.h b/modules/gdscript/gd_functions.h index 8c88472567..3a993cc038 100644 --- a/modules/gdscript/gd_functions.h +++ b/modules/gdscript/gd_functions.h @@ -92,6 +92,7 @@ public: VAR_TO_BYTES, BYTES_TO_VAR, GEN_RANGE, + GEN_XRANGE, RESOURCE_LOAD, INST2DICT, DICT2INST, |