diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2019-09-02 15:28:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-02 15:28:15 +0200 |
commit | d767edb9b3439a200caf07d7ec6dd3e2b9495ac1 (patch) | |
tree | 864a23a6f006af73752858a9eb95a0a5b5260cca | |
parent | 7e731bbce221c423340821f1801a4fe021138358 (diff) | |
parent | 5565f5591f1096870327d893f8539eff22d17e68 (diff) |
Merge pull request #21443 from deepmax/ord_function
Add ord() function to return Unicode code point of a string of length one
-rw-r--r-- | modules/gdscript/gdscript_functions.cpp | 36 | ||||
-rw-r--r-- | modules/gdscript/gdscript_functions.h | 1 |
2 files changed, 37 insertions, 0 deletions
diff --git a/modules/gdscript/gdscript_functions.cpp b/modules/gdscript/gdscript_functions.cpp index ad8bf5b2c0..97790e00bb 100644 --- a/modules/gdscript/gdscript_functions.cpp +++ b/modules/gdscript/gdscript_functions.cpp @@ -106,6 +106,7 @@ const char *GDScriptFunctions::get_func_name(Function p_func) { "typeof", "type_exists", "char", + "ord", "str", "print", "printt", @@ -665,6 +666,33 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_ CharType result[2] = { *p_args[0], 0 }; r_ret = String(result); } break; + case TEXT_ORD: { + + VALIDATE_ARG_COUNT(1); + + if (p_args[0]->get_type() != Variant::STRING) { + + r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::STRING; + r_ret = Variant(); + return; + } + + String str = p_args[0]->operator String(); + + if (str.length() != 1) { + + r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::STRING; + r_ret = RTR("Expected a string of length 1 (a character)."); + return; + } + + r_ret = str.get(0); + + } break; case TEXT_STR: { if (p_arg_count < 1) { r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; @@ -1507,6 +1535,7 @@ bool GDScriptFunctions::is_deterministic(Function p_func) { case TYPE_OF: case TYPE_EXISTS: case TEXT_CHAR: + case TEXT_ORD: case TEXT_STR: case COLOR8: case LEN: @@ -1849,6 +1878,13 @@ MethodInfo GDScriptFunctions::get_info(Function p_func) { return mi; } break; + case TEXT_ORD: { + + MethodInfo mi("ord", PropertyInfo(Variant::STRING, "char")); + mi.return_val.type = Variant::INT; + return mi; + + } break; case TEXT_STR: { MethodInfo mi("str"); diff --git a/modules/gdscript/gdscript_functions.h b/modules/gdscript/gdscript_functions.h index 8f7ba76d2c..9ea5dd46cf 100644 --- a/modules/gdscript/gdscript_functions.h +++ b/modules/gdscript/gdscript_functions.h @@ -97,6 +97,7 @@ public: TYPE_OF, TYPE_EXISTS, TEXT_CHAR, + TEXT_ORD, TEXT_STR, TEXT_PRINT, TEXT_PRINT_TABBED, |