diff --git a/core/math/expression.cpp b/core/math/expression.cpp index dbead6b47..a1f55620d 100644 --- a/core/math/expression.cpp +++ b/core/math/expression.cpp @@ -2209,6 +2209,33 @@ Variant Expression::execute(Array p_inputs, Object *p_base, bool p_show_error) { return output; } +Array Expression::execute_arr(Array p_inputs, Object *p_base, bool p_show_error) { + Array ret; + ret.resize(3); + + ERR_FAIL_COND_V_MSG(error_set, ret, "There was previously a parse error: " + error_str + "."); + + execution_error = false; + Variant output; + String error_txt; + + bool err = _execute(p_inputs, p_base, root, output, error_txt); + + ret[0] = output; + + if (err) { + ret[1] = true; + ret[2] = error_txt; + + ERR_FAIL_COND_V_MSG(p_show_error, ret, error_str); + } else { + ret[1] = false; + ret[2] = ""; + } + + return ret; +} + bool Expression::has_execute_failed() const { return execution_error; } @@ -2220,6 +2247,7 @@ String Expression::get_error_text() const { void Expression::_bind_methods() { ClassDB::bind_method(D_METHOD("parse", "expression", "input_names"), &Expression::parse, DEFVAL(Vector())); ClassDB::bind_method(D_METHOD("execute", "inputs", "base_instance", "show_error"), &Expression::execute, DEFVAL(Array()), DEFVAL(Variant()), DEFVAL(true)); + ClassDB::bind_method(D_METHOD("execute_arr", "inputs", "base_instance", "show_error"), &Expression::execute_arr, DEFVAL(Array()), DEFVAL(Variant()), DEFVAL(true)); ClassDB::bind_method(D_METHOD("has_execute_failed"), &Expression::has_execute_failed); ClassDB::bind_method(D_METHOD("get_error_text"), &Expression::get_error_text); } diff --git a/core/math/expression.h b/core/math/expression.h index 7a384ac06..840269bbc 100644 --- a/core/math/expression.h +++ b/core/math/expression.h @@ -34,6 +34,8 @@ #include "core/object/reference.h" +#include "core/variant/array.h" + class Expression : public Reference { GDCLASS(Expression, Reference); @@ -358,7 +360,10 @@ protected: public: Error parse(const String &p_expression, const Vector &p_input_names = Vector()); + Variant execute(Array p_inputs, Object *p_base = nullptr, bool p_show_error = true); + Array execute_arr(Array p_inputs, Object *p_base = nullptr, bool p_show_error = true); + bool has_execute_failed() const; String get_error_text() const; diff --git a/doc/classes/Expression.xml b/doc/classes/Expression.xml index 234ebbddc..541e5ad86 100644 --- a/doc/classes/Expression.xml +++ b/doc/classes/Expression.xml @@ -1,5 +1,5 @@ - + A class that stores an expression you can execute. @@ -36,6 +36,15 @@ If you defined input variables in [method parse], you can specify their values in the inputs array, in the same order. + + + + + + + Same as [method execute] except it does not set member variables. ret[0] is the result, ret[1] is a bool telling you whether execute was successful or not, ret[2] is a String contanining the error string if there was any. + +