summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorJuan Linietsky <reduzio@gmail.com>2017-08-07 18:34:18 -0300
committerJuan Linietsky <reduzio@gmail.com>2017-08-07 18:38:47 -0300
commitc6120e77a8c1c7e946569453973629499673d414 (patch)
tree7a43f26d5c61a1f1cc698b277c24836cb1e1cc9c /modules
parent0188ce5c80b141d0f8a5880d3253c0b5eeb91e4f (diff)
Implement len() gdscript built-in function for python users, closes #1960
Diffstat (limited to 'modules')
-rw-r--r--modules/gdscript/gd_functions.cpp63
-rw-r--r--modules/gdscript/gd_functions.h1
2 files changed, 64 insertions, 0 deletions
diff --git a/modules/gdscript/gd_functions.cpp b/modules/gdscript/gd_functions.cpp
index 8bc3b24a5e..209bdadd67 100644
--- a/modules/gdscript/gd_functions.cpp
+++ b/modules/gdscript/gd_functions.cpp
@@ -113,6 +113,7 @@ const char *GDFunctions::get_func_name(Function p_func) {
"ColorN",
"print_stack",
"instance_from_id",
+ "len",
};
return _names[p_func];
@@ -1154,6 +1155,62 @@ void GDFunctions::call(Function p_func, const Variant **p_args, int p_arg_count,
r_ret = ObjectDB::get_instance(id);
} break;
+ case LEN: {
+
+ VALIDATE_ARG_COUNT(1);
+ switch (p_args[0]->get_type()) {
+ case Variant::DICTIONARY: {
+ Dictionary d = *p_args[0];
+ r_ret = d.size();
+ } break;
+ case Variant::ARRAY: {
+ Array d = *p_args[0];
+ r_ret = d.size();
+ } break;
+ case Variant::POOL_BYTE_ARRAY: {
+ PoolVector<uint8_t> d = *p_args[0];
+ r_ret = d.size();
+
+ } break;
+ case Variant::POOL_INT_ARRAY: {
+ PoolVector<int> d = *p_args[0];
+ r_ret = d.size();
+ } break;
+ case Variant::POOL_REAL_ARRAY: {
+
+ PoolVector<real_t> d = *p_args[0];
+ r_ret = d.size();
+ } break;
+ case Variant::POOL_STRING_ARRAY: {
+ PoolVector<String> d = *p_args[0];
+ r_ret = d.size();
+
+ } break;
+ case Variant::POOL_VECTOR2_ARRAY: {
+ PoolVector<Vector2> d = *p_args[0];
+ r_ret = d.size();
+
+ } break;
+ case Variant::POOL_VECTOR3_ARRAY: {
+
+ PoolVector<Vector3> d = *p_args[0];
+ r_ret = d.size();
+ } break;
+ case Variant::POOL_COLOR_ARRAY: {
+
+ PoolVector<Color> d = *p_args[0];
+ r_ret = d.size();
+ } break;
+ default: {
+ r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
+ r_error.argument = 0;
+ r_error.expected = Variant::OBJECT;
+ r_ret = Variant();
+ r_ret = RTR("Object can't provide a length.");
+ }
+ }
+
+ } break;
case FUNC_MAX: {
ERR_FAIL();
@@ -1210,6 +1267,7 @@ bool GDFunctions::is_deterministic(Function p_func) {
case TEXT_CHAR:
case TEXT_STR:
case COLOR8:
+ case LEN:
// enable for debug only, otherwise not desirable - case GEN_RANGE:
return true;
default:
@@ -1621,6 +1679,11 @@ MethodInfo GDFunctions::get_info(Function p_func) {
mi.return_val.type = Variant::OBJECT;
return mi;
} break;
+ case LEN: {
+ MethodInfo mi("len", PropertyInfo(Variant::NIL, "var"));
+ mi.return_val.type = Variant::INT;
+ return mi;
+ } break;
case FUNC_MAX: {
diff --git a/modules/gdscript/gd_functions.h b/modules/gdscript/gd_functions.h
index 4d52abaeab..93cb524118 100644
--- a/modules/gdscript/gd_functions.h
+++ b/modules/gdscript/gd_functions.h
@@ -105,6 +105,7 @@ public:
COLORN,
PRINT_STACK,
INSTANCE_FROM_ID,
+ LEN,
FUNC_MAX
};