diff options
author | Danil Alexeev <danil@alexeev.xyz> | 2019-12-29 09:20:10 +0300 |
---|---|---|
committer | Danil Alexeev <danil@alexeev.xyz> | 2019-12-29 09:20:10 +0300 |
commit | 134755ebcf0f684f51c1c50562c937296f16ce33 (patch) | |
tree | 286080edb20e728f552df9a835507271e5469fde /core | |
parent | 318c69351624f7794c51b5385d252af397c0404a (diff) |
Add ord() function to Expression class
The ord() function was recently added in GDScript and VisualScript,
but was missed in the Expression class.
Diffstat (limited to 'core')
-rw-r--r-- | core/math/expression.cpp | 28 | ||||
-rw-r--r-- | core/math/expression.h | 1 |
2 files changed, 29 insertions, 0 deletions
diff --git a/core/math/expression.cpp b/core/math/expression.cpp index 46f81ce5c3..568893e905 100644 --- a/core/math/expression.cpp +++ b/core/math/expression.cpp @@ -98,6 +98,7 @@ const char *Expression::func_name[Expression::FUNC_MAX] = { "typeof", "type_exists", "char", + "ord", "str", "print", "printerr", @@ -164,6 +165,7 @@ int Expression::get_func_argument_count(BuiltinFunc p_func) { case OBJ_WEAKREF: case TYPE_OF: case TEXT_CHAR: + case TEXT_ORD: case TEXT_STR: case TEXT_PRINT: case TEXT_PRINTERR: @@ -676,6 +678,32 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant *r_return = String(result); } break; + case TEXT_ORD: { + + if (p_inputs[0]->get_type() != Variant::STRING) { + + r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::STRING; + + return; + } + + String str = *p_inputs[0]; + + if (str.length() != 1) { + + r_error_str = RTR("Expected a string of length 1 (a character)."); + r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::STRING; + + return; + } + + *r_return = str.get(0); + + } break; case TEXT_STR: { String str = *p_inputs[0]; diff --git a/core/math/expression.h b/core/math/expression.h index 833220592c..aac27276d7 100644 --- a/core/math/expression.h +++ b/core/math/expression.h @@ -97,6 +97,7 @@ public: TYPE_OF, TYPE_EXISTS, TEXT_CHAR, + TEXT_ORD, TEXT_STR, TEXT_PRINT, TEXT_PRINTERR, |