Added an is_valid function to FuncRef so script can check if it is safe to call it.
This commit is contained in:
parent
7018de8425
commit
9be8424ef9
2 changed files with 14 additions and 0 deletions
|
@ -51,11 +51,23 @@ void FuncRef::set_instance(Object *p_obj) {
|
||||||
ERR_FAIL_NULL(p_obj);
|
ERR_FAIL_NULL(p_obj);
|
||||||
id = p_obj->get_instance_id();
|
id = p_obj->get_instance_id();
|
||||||
}
|
}
|
||||||
|
|
||||||
void FuncRef::set_function(const StringName &p_func) {
|
void FuncRef::set_function(const StringName &p_func) {
|
||||||
|
|
||||||
function = p_func;
|
function = p_func;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool FuncRef::is_valid() const {
|
||||||
|
if (id == 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Object *obj = ObjectDB::get_instance(id);
|
||||||
|
if (!obj)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return obj->has_method(function);
|
||||||
|
}
|
||||||
|
|
||||||
void FuncRef::_bind_methods() {
|
void FuncRef::_bind_methods() {
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -67,6 +79,7 @@ void FuncRef::_bind_methods() {
|
||||||
|
|
||||||
ClassDB::bind_method(D_METHOD("set_instance", "instance"), &FuncRef::set_instance);
|
ClassDB::bind_method(D_METHOD("set_instance", "instance"), &FuncRef::set_instance);
|
||||||
ClassDB::bind_method(D_METHOD("set_function", "name"), &FuncRef::set_function);
|
ClassDB::bind_method(D_METHOD("set_function", "name"), &FuncRef::set_function);
|
||||||
|
ClassDB::bind_method(D_METHOD("is_valid"), &FuncRef::is_valid);
|
||||||
}
|
}
|
||||||
|
|
||||||
FuncRef::FuncRef() :
|
FuncRef::FuncRef() :
|
||||||
|
|
|
@ -46,6 +46,7 @@ public:
|
||||||
Variant call_func(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
|
Variant call_func(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
|
||||||
void set_instance(Object *p_obj);
|
void set_instance(Object *p_obj);
|
||||||
void set_function(const StringName &p_func);
|
void set_function(const StringName &p_func);
|
||||||
|
bool is_valid() const;
|
||||||
FuncRef();
|
FuncRef();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue