Use Callable in Thread::start
This commit is contained in:
parent
9293c76636
commit
97c68514c4
|
@ -1786,13 +1786,13 @@ void Thread::_start_func(void *ud) {
|
|||
// We must check if we are in case b).
|
||||
int target_param_count = 0;
|
||||
int target_default_arg_count = 0;
|
||||
Ref<Script> script = t->target_instance->get_script();
|
||||
Ref<Script> script = t->target_callable.get_object()->get_script();
|
||||
if (script.is_valid()) {
|
||||
MethodInfo mi = script->get_method_info(t->target_method);
|
||||
MethodInfo mi = script->get_method_info(t->target_callable.get_method());
|
||||
target_param_count = mi.arguments.size();
|
||||
target_default_arg_count = mi.default_arguments.size();
|
||||
} else {
|
||||
MethodBind *method = ClassDB::get_method(t->target_instance->get_class_name(), t->target_method);
|
||||
MethodBind *method = ClassDB::get_method(t->target_callable.get_object()->get_class_name(), t->target_callable.get_method());
|
||||
target_param_count = method->get_argument_count();
|
||||
target_default_arg_count = method->get_default_argument_count();
|
||||
}
|
||||
|
@ -1801,41 +1801,21 @@ void Thread::_start_func(void *ud) {
|
|||
}
|
||||
}
|
||||
|
||||
::Thread::set_name(t->target_method);
|
||||
::Thread::set_name(t->target_callable.get_method());
|
||||
|
||||
t->ret = t->target_instance->call(t->target_method, arg, argc, ce);
|
||||
t->target_callable.call(arg, argc, t->ret, ce);
|
||||
if (ce.error != Callable::CallError::CALL_OK) {
|
||||
String reason;
|
||||
switch (ce.error) {
|
||||
case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
|
||||
reason = "Invalid Argument #" + itos(ce.argument);
|
||||
} break;
|
||||
case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
|
||||
reason = "Too Many Arguments";
|
||||
} break;
|
||||
case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
|
||||
reason = "Too Few Arguments";
|
||||
} break;
|
||||
case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
|
||||
reason = "Method Not Found";
|
||||
} break;
|
||||
default: {
|
||||
}
|
||||
}
|
||||
|
||||
ERR_FAIL_MSG("Could not call function '" + t->target_method.operator String() + "' to start thread " + t->get_id() + ": " + reason + ".");
|
||||
ERR_FAIL_MSG("Could not call function '" + t->target_callable.get_method().operator String() + "' to start thread " + t->get_id() + ": " + Variant::get_callable_error_text(t->target_callable, arg, argc, ce) + ".");
|
||||
}
|
||||
}
|
||||
|
||||
Error Thread::start(Object *p_instance, const StringName &p_method, const Variant &p_userdata, Priority p_priority) {
|
||||
Error Thread::start(const Callable &p_callable, const Variant &p_userdata, Priority p_priority) {
|
||||
ERR_FAIL_COND_V_MSG(active.is_set(), ERR_ALREADY_IN_USE, "Thread already started.");
|
||||
ERR_FAIL_COND_V(!p_instance, ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_COND_V(p_method == StringName(), ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_COND_V(p_callable.is_null(), ERR_INVALID_PARAMETER);
|
||||
ERR_FAIL_INDEX_V(p_priority, PRIORITY_MAX, ERR_INVALID_PARAMETER);
|
||||
|
||||
ret = Variant();
|
||||
target_method = p_method;
|
||||
target_instance = p_instance;
|
||||
target_callable = p_callable;
|
||||
userdata = p_userdata;
|
||||
active.set();
|
||||
|
||||
|
@ -1861,15 +1841,14 @@ Variant Thread::wait_to_finish() {
|
|||
thread.wait_to_finish();
|
||||
Variant r = ret;
|
||||
active.clear();
|
||||
target_method = StringName();
|
||||
target_instance = nullptr;
|
||||
target_callable = Callable();
|
||||
userdata = Variant();
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
void Thread::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("start", "instance", "method", "userdata", "priority"), &Thread::start, DEFVAL(Variant()), DEFVAL(PRIORITY_NORMAL));
|
||||
ClassDB::bind_method(D_METHOD("start", "callable", "userdata", "priority"), &Thread::start, DEFVAL(Variant()), DEFVAL(PRIORITY_NORMAL));
|
||||
ClassDB::bind_method(D_METHOD("get_id"), &Thread::get_id);
|
||||
ClassDB::bind_method(D_METHOD("is_active"), &Thread::is_active);
|
||||
ClassDB::bind_method(D_METHOD("wait_to_finish"), &Thread::wait_to_finish);
|
||||
|
|
|
@ -539,8 +539,7 @@ protected:
|
|||
Variant ret;
|
||||
Variant userdata;
|
||||
SafeFlag active;
|
||||
Object *target_instance = nullptr;
|
||||
StringName target_method;
|
||||
Callable target_callable;
|
||||
::Thread thread;
|
||||
static void _bind_methods();
|
||||
static void _start_func(void *ud);
|
||||
|
@ -553,7 +552,7 @@ public:
|
|||
PRIORITY_MAX
|
||||
};
|
||||
|
||||
Error start(Object *p_instance, const StringName &p_method, const Variant &p_userdata = Variant(), Priority p_priority = PRIORITY_NORMAL);
|
||||
Error start(const Callable &p_callable, const Variant &p_userdata = Variant(), Priority p_priority = PRIORITY_NORMAL);
|
||||
String get_id() const;
|
||||
bool is_active() const;
|
||||
Variant wait_to_finish();
|
||||
|
|
|
@ -27,12 +27,11 @@
|
|||
</method>
|
||||
<method name="start">
|
||||
<return type="int" enum="Error" />
|
||||
<argument index="0" name="instance" type="Object" />
|
||||
<argument index="1" name="method" type="StringName" />
|
||||
<argument index="2" name="userdata" type="Variant" default="null" />
|
||||
<argument index="3" name="priority" type="int" enum="Thread.Priority" default="1" />
|
||||
<argument index="0" name="callable" type="Callable" />
|
||||
<argument index="1" name="userdata" type="Variant" default="null" />
|
||||
<argument index="2" name="priority" type="int" enum="Thread.Priority" default="1" />
|
||||
<description>
|
||||
Starts a new [Thread] that runs [code]method[/code] on object [code]instance[/code] with [code]userdata[/code] passed as an argument. Even if no userdata is passed, [code]method[/code] must accept one argument and it will be null. The [code]priority[/code] of the [Thread] can be changed by passing a value from the [enum Priority] enum.
|
||||
Starts a new [Thread] that calls [code]callable[/code] with [code]userdata[/code] passed as an argument. Even if no userdata is passed, [code]method[/code] must accept one argument and it will be null. The [code]priority[/code] of the [Thread] can be changed by passing a value from the [enum Priority] enum.
|
||||
Returns [constant OK] on success, or [constant ERR_CANT_CREATE] on failure.
|
||||
</description>
|
||||
</method>
|
||||
|
|
Loading…
Reference in New Issue