Browse Source

Add ord() function to Expression class

The ord() function was recently added in GDScript and VisualScript,
but was missed in the Expression class.
Danil Alexeev 5 years ago
parent
commit
134755ebcf
2 changed files with 29 additions and 0 deletions
  1. 28 0
      core/math/expression.cpp
  2. 1 0
      core/math/expression.h

+ 28 - 0
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: {
 

+ 1 - 0
core/math/expression.h

@@ -97,6 +97,7 @@ public:
 		TYPE_OF,
 		TYPE_EXISTS,
 		TEXT_CHAR,
+		TEXT_ORD,
 		TEXT_STR,
 		TEXT_PRINT,
 		TEXT_PRINTERR,