Implement len() gdscript built-in function for python users, closes #1960

This commit is contained in:
Juan Linietsky 2017-08-07 18:34:18 -03:00
parent 0188ce5c80
commit c6120e77a8
2 changed files with 64 additions and 0 deletions

View File

@ -113,6 +113,7 @@ const char *GDFunctions::get_func_name(Function p_func) {
"ColorN",
"print_stack",
"instance_from_id",
"len",
};
return _names[p_func];
@ -1153,6 +1154,62 @@ void GDFunctions::call(Function p_func, const Variant **p_args, int p_arg_count,
uint32_t id = *p_args[0];
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: {
@ -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: {

View File

@ -105,6 +105,7 @@ public:
COLORN,
PRINT_STACK,
INSTANCE_FROM_ID,
LEN,
FUNC_MAX
};