Added a new method to Expression so it can be used with multiple threads simultenously.

This commit is contained in:
Relintai 2024-02-25 10:25:46 +01:00
parent 4b9982d2d9
commit 66c2b12fc0
3 changed files with 43 additions and 1 deletions

View File

@ -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<String>()));
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);
}

View File

@ -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<String> &p_input_names = Vector<String>());
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;

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="Expression" inherits="Reference" version="4.2">
<class name="Expression" inherits="Reference" version="4.3">
<brief_description>
A class that stores an expression you can execute.
</brief_description>
@ -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.
</description>
</method>
<method name="execute_arr">
<return type="Array" />
<argument index="0" name="inputs" type="Array" default="[ ]" />
<argument index="1" name="base_instance" type="Object" default="null" />
<argument index="2" name="show_error" type="bool" default="true" />
<description>
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.
</description>
</method>
<method name="get_error_text" qualifiers="const">
<return type="String" />
<description>