Skip to content

Commit dd7cb05

Browse files
committed
Merge pull request godotengine#87344 from AThousandShips/signal_connected
[Core] Add way to check if a signal has any connections
2 parents 621cadc + 203d3be commit dd7cb05

File tree

9 files changed

+49
-0
lines changed

9 files changed

+49
-0
lines changed

core/object/object.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,24 @@ bool Object::is_connected(const StringName &p_signal, const Callable &p_callable
14571457
return s->slot_map.has(*p_callable.get_base_comparator());
14581458
}
14591459

1460+
bool Object::has_connections(const StringName &p_signal) const {
1461+
const SignalData *s = signal_map.getptr(p_signal);
1462+
if (!s) {
1463+
bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
1464+
if (signal_is_valid) {
1465+
return false;
1466+
}
1467+
1468+
if (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal)) {
1469+
return false;
1470+
}
1471+
1472+
ERR_FAIL_V_MSG(false, "Nonexistent signal: " + p_signal + ".");
1473+
}
1474+
1475+
return !s->slot_map.is_empty();
1476+
}
1477+
14601478
void Object::disconnect(const StringName &p_signal, const Callable &p_callable) {
14611479
_disconnect(p_signal, p_callable);
14621480
}
@@ -1697,6 +1715,7 @@ void Object::_bind_methods() {
16971715
ClassDB::bind_method(D_METHOD("connect", "signal", "callable", "flags"), &Object::connect, DEFVAL(0));
16981716
ClassDB::bind_method(D_METHOD("disconnect", "signal", "callable"), &Object::disconnect);
16991717
ClassDB::bind_method(D_METHOD("is_connected", "signal", "callable"), &Object::is_connected);
1718+
ClassDB::bind_method(D_METHOD("has_connections", "signal"), &Object::has_connections);
17001719

17011720
ClassDB::bind_method(D_METHOD("set_block_signals", "enable"), &Object::set_block_signals);
17021721
ClassDB::bind_method(D_METHOD("is_blocking_signals"), &Object::is_blocking_signals);

core/object/object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,7 @@ class Object {
932932
MTVIRTUAL Error connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags = 0);
933933
MTVIRTUAL void disconnect(const StringName &p_signal, const Callable &p_callable);
934934
MTVIRTUAL bool is_connected(const StringName &p_signal, const Callable &p_callable) const;
935+
MTVIRTUAL bool has_connections(const StringName &p_signal) const;
935936

936937
template <typename... VarArgs>
937938
void call_deferred(const StringName &p_name, VarArgs... p_args) {

core/variant/callable.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,13 @@ bool Signal::is_connected(const Callable &p_callable) const {
545545
return obj->is_connected(name, p_callable);
546546
}
547547

548+
bool Signal::has_connections() const {
549+
Object *obj = get_object();
550+
ERR_FAIL_NULL_V(obj, false);
551+
552+
return obj->has_connections(name);
553+
}
554+
548555
Array Signal::get_connections() const {
549556
Object *obj = get_object();
550557
if (!obj) {

core/variant/callable.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ class Signal {
192192
Error connect(const Callable &p_callable, uint32_t p_flags = 0);
193193
void disconnect(const Callable &p_callable);
194194
bool is_connected(const Callable &p_callable) const;
195+
bool has_connections() const;
195196

196197
Array get_connections() const;
197198
Signal(const Object *p_object, const StringName &p_name);

core/variant/variant_call.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,7 @@ static void _register_variant_builtin_methods_misc() {
21212121
bind_method(Signal, disconnect, sarray("callable"), varray());
21222122
bind_method(Signal, is_connected, sarray("callable"), varray());
21232123
bind_method(Signal, get_connections, sarray(), varray());
2124+
bind_method(Signal, has_connections, sarray(), varray());
21242125

21252126
bind_custom(Signal, emit, _VariantCall::func_Signal_emit, false, Variant);
21262127

doc/classes/Object.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,14 @@
824824
Returns the name of the translation domain used by [method tr] and [method tr_n]. See also [TranslationServer].
825825
</description>
826826
</method>
827+
<method name="has_connections" qualifiers="const">
828+
<return type="bool" />
829+
<param index="0" name="signal" type="StringName" />
830+
<description>
831+
Returns [code]true[/code] if any connection exists on the given [param signal] name.
832+
[b]Note:[/b] In C#, [param signal] must be in snake_case when referring to built-in Godot methods. Prefer using the names exposed in the [code]SignalName[/code] class to avoid allocating a new [StringName] on each call.
833+
</description>
834+
</method>
827835
<method name="has_meta" qualifiers="const">
828836
<return type="bool" />
829837
<param index="0" name="name" type="StringName" />

doc/classes/Signal.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@
109109
Returns the ID of the object emitting this signal (see [method Object.get_instance_id]).
110110
</description>
111111
</method>
112+
<method name="has_connections" qualifiers="const">
113+
<return type="bool" />
114+
<description>
115+
Returns [code]true[/code] if any [Callable] is connected to this signal.
116+
</description>
117+
</method>
112118
<method name="is_connected" qualifiers="const">
113119
<return type="bool" />
114120
<param index="0" name="callable" type="Callable" />

scene/main/node.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4019,4 +4019,9 @@ bool Node::is_connected(const StringName &p_signal, const Callable &p_callable)
40194019
return Object::is_connected(p_signal, p_callable);
40204020
}
40214021

4022+
bool Node::has_connections(const StringName &p_signal) const {
4023+
ERR_THREAD_GUARD_V(false);
4024+
return Object::has_connections(p_signal);
4025+
}
4026+
40224027
#endif

scene/main/node.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,7 @@ class Node : public Object {
797797
virtual Error connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags = 0) override;
798798
virtual void disconnect(const StringName &p_signal, const Callable &p_callable) override;
799799
virtual bool is_connected(const StringName &p_signal, const Callable &p_callable) const override;
800+
virtual bool has_connections(const StringName &p_signal) const override;
800801
#endif
801802
Node();
802803
~Node();

0 commit comments

Comments
 (0)