summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <remi@verschelde.fr>2021-07-01 12:57:14 +0200
committerGitHub <noreply@github.com>2021-07-01 12:57:14 +0200
commit230a24a49d1987489f44bdd4a3cdaa34d3264437 (patch)
treeb428c300218ba1fbf63cb50d474e3fe44e7d6b4d
parent7ee419f8d97657df558cd9e0eab4671a36f3c21f (diff)
parent8f1efa656b2e91c159dff54a7b3af7bfb4659879 (diff)
Merge pull request #50026 from lyuma/callable_is_valid
Add Callable.is_valid() analogous to FuncRef.is_valid() from 3.x
-rw-r--r--core/variant/callable.cpp4
-rw-r--r--core/variant/callable.h1
-rw-r--r--core/variant/variant_call.cpp1
-rw-r--r--doc/classes/Callable.xml7
4 files changed, 13 insertions, 0 deletions
diff --git a/core/variant/callable.cpp b/core/variant/callable.cpp
index 34b3e3ea35..ca6f3d615e 100644
--- a/core/variant/callable.cpp
+++ b/core/variant/callable.cpp
@@ -89,6 +89,10 @@ Callable Callable::unbind(int p_argcount) const {
return Callable(memnew(CallableCustomUnbind(*this, p_argcount)));
}
+bool Callable::is_valid() const {
+ return get_object() && (is_custom() || get_object()->has_method(get_method()));
+}
+
Object *Callable::get_object() const {
if (is_null()) {
return nullptr;
diff --git a/core/variant/callable.h b/core/variant/callable.h
index 20d0804292..52094af3aa 100644
--- a/core/variant/callable.h
+++ b/core/variant/callable.h
@@ -81,6 +81,7 @@ public:
_FORCE_INLINE_ bool is_standard() const {
return method != StringName();
}
+ bool is_valid() const;
Callable bind(const Variant **p_arguments, int p_argcount) const;
Callable unbind(int p_argcount) const;
diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp
index 95c4e7121b..733361fe58 100644
--- a/core/variant/variant_call.cpp
+++ b/core/variant/variant_call.cpp
@@ -1630,6 +1630,7 @@ static void _register_variant_builtin_methods() {
bind_method(Callable, is_null, sarray(), varray());
bind_method(Callable, is_custom, sarray(), varray());
bind_method(Callable, is_standard, sarray(), varray());
+ bind_method(Callable, is_valid, sarray(), varray());
bind_method(Callable, get_object, sarray(), varray());
bind_method(Callable, get_object_id, sarray(), varray());
bind_method(Callable, get_method, sarray(), varray());
diff --git a/doc/classes/Callable.xml b/doc/classes/Callable.xml
index cbab1a8f50..3c61e8278f 100644
--- a/doc/classes/Callable.xml
+++ b/doc/classes/Callable.xml
@@ -133,6 +133,13 @@
Returns [code]true[/code] if this [Callable] is a standard callable, referencing an object and a method using a [StringName].
</description>
</method>
+ <method name="is_valid" qualifiers="const">
+ <return type="bool">
+ </return>
+ <description>
+ Returns [code]true[/code] if the object exists and has a valid function assigned, or is a custom callable.
+ </description>
+ </method>
<method name="operator !=" qualifiers="operator">
<return type="bool">
</return>