Skip to content

Commit

Permalink
Implement len() gdscript built-in function for python users, closes g…
Browse files Browse the repository at this point in the history
  • Loading branch information
reduz committed Aug 7, 2017
1 parent 0188ce5 commit c6120e7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
63 changes: 63 additions & 0 deletions modules/gdscript/gd_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ const char *GDFunctions::get_func_name(Function p_func) {
"ColorN",
"print_stack",
"instance_from_id",
"len",
};

return _names[p_func];
Expand Down Expand Up @@ -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: {

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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: {

Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gd_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class GDFunctions {
COLORN,
PRINT_STACK,
INSTANCE_FROM_ID,
LEN,
FUNC_MAX

};
Expand Down

0 comments on commit c6120e7

Please sign in to comment.