From 134755ebcf0f684f51c1c50562c937296f16ce33 Mon Sep 17 00:00:00 2001 From: Danil Alexeev Date: Sun, 29 Dec 2019 09:20:10 +0300 Subject: Add ord() function to Expression class The ord() function was recently added in GDScript and VisualScript, but was missed in the Expression class. --- core/math/expression.cpp | 28 ++++++++++++++++++++++++++++ core/math/expression.h | 1 + 2 files changed, 29 insertions(+) (limited to 'core') 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: @@ -675,6 +677,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: { 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, -- cgit v1.2.3