summaryrefslogtreecommitdiff
path: root/core/func_ref.cpp
diff options
context:
space:
mode:
authorRikhardur Bjarni Einarsson <badulf96@gmail.com>2019-04-29 23:38:42 +0100
committerRikhardur Bjarni Einarsson <badulf96@gmail.com>2019-04-29 23:38:42 +0100
commit9be8424ef980d7a85e08535de986123033ba55aa (patch)
treef4d72797003dec70d9916f46a71d93959f886f21 /core/func_ref.cpp
parent7018de8425e8c2781071595fdcf565acdfde2be4 (diff)
Added an is_valid function to FuncRef so script can check if it is safe to call it.
Diffstat (limited to 'core/func_ref.cpp')
-rw-r--r--core/func_ref.cpp13
1 files changed, 13 insertions, 0 deletions
diff --git a/core/func_ref.cpp b/core/func_ref.cpp
index 4a965473d9..3d03137d09 100644
--- a/core/func_ref.cpp
+++ b/core/func_ref.cpp
@@ -51,11 +51,23 @@ void FuncRef::set_instance(Object *p_obj) {
ERR_FAIL_NULL(p_obj);
id = p_obj->get_instance_id();
}
+
void FuncRef::set_function(const StringName &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() {
{
@@ -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_function", "name"), &FuncRef::set_function);
+ ClassDB::bind_method(D_METHOD("is_valid"), &FuncRef::is_valid);
}
FuncRef::FuncRef() :