Struct TypedSignal
pub struct TypedSignal<'c, C, Ps>where
C: WithSignals,{ /* private fields */ }
Expand description
Type-safe version of a Godot signal.
Short-lived type, only valid in the scope of its surrounding object type C
, for lifetime 'c
. The generic argument Ps
represents
the parameters of the signal, thus ensuring the type safety.
See the Signals chapter in the book for a general introduction and examples.
§Listing signals of a class
The WithSignals::SignalCollection
struct stores multiple signals with distinct, code-generated types, but they all implement
Deref
and DerefMut
to TypedSignal
. This allows you to either use the concrete APIs of the generated types, or the more generic
ones of TypedSignal
.
You can access the signal collection of a class via self.signals()
or
Gd::signals()
.
§Connecting a signal to a receiver
Receiver functions are functions that are called when a signal is emitted. You can connect a signal in many different ways:
connect()
: Connect a global/associated function or a closure.connect_self()
: Connect a method or closure that runs on the signal emitter.connect_other()
: Connect a method or closure that runs on a separate object.builder()
for more complex setups (such as choosingConnectFlags
or making thread-safe connections).
§Emitting a signal
Code-generated signal types provide a method emit(...)
, which adopts the names and types of the #[signal]
parameter list.
In most cases, that’s the method you are looking for.
For generic use, you can also use emit_tuple()
, which does not provide parameter names.
§Generic programming and code reuse
If you want to build higher-level abstractions that operate on TypedSignal
, you will need the SignalReceiver
trait.
Implementations§
§impl<'c, C, Ps> TypedSignal<'c, C, Ps>where
C: WithSignals,
Ps: ParamTuple,
impl<'c, C, Ps> TypedSignal<'c, C, Ps>where
C: WithSignals,
Ps: ParamTuple,
pub fn builder<'ts>(&'ts self) -> ConnectBuilder<'ts, 'c, C, Ps>
pub fn builder<'ts>(&'ts self) -> ConnectBuilder<'ts, 'c, C, Ps>
Fully customizable connection setup.
The returned builder provides several methods to configure how to connect the signal. It needs to be finalized with a call
to any of the builder’s connect_*
methods.
pub fn emit_tuple(&mut self, args: Ps)where
Ps: OutParamTuple,
pub fn emit_tuple(&mut self, args: Ps)where
Ps: OutParamTuple,
Emit the signal with the given parameters.
This is intended for generic use. Typically, you’ll want to use the more specific emit()
method of the code-generated signal
type, which also has named parameters.
§impl<C, Ps> TypedSignal<'_, C, Ps>where
C: WithSignals,
Ps: InParamTuple + 'static,
impl<C, Ps> TypedSignal<'_, C, Ps>where
C: WithSignals,
Ps: InParamTuple + 'static,
pub fn connect<F>(&self, function: F) -> ConnectHandlewhere
F: for<'c_rcv> SignalReceiver<(), Ps> + 'static,
IndirectSignalReceiver<'c_rcv, (), Ps, F>: for<'c_rcv> From<&'c_rcv mut F>,
pub fn connect<F>(&self, function: F) -> ConnectHandlewhere
F: for<'c_rcv> SignalReceiver<(), Ps> + 'static,
IndirectSignalReceiver<'c_rcv, (), Ps, F>: for<'c_rcv> From<&'c_rcv mut F>,
Connect a non-member function (global function, associated function or closure).
Example usages:
sig.connect(Self::static_func);
sig.connect(global_func);
sig.connect(|arg| { /* closure */ });
- To connect to a method on the object that owns this signal, use
connect_self()
. - If you need
connect flags
or cross-thread signals, usebuilder()
.
pub fn connect_self<F, Declarer>(&self, function: F) -> ConnectHandlewhere
F: for<'c_rcv> SignalReceiver<&'c_rcv mut C, Ps> + 'static,
IndirectSignalReceiver<'c_rcv, &'c_rcv mut C, Ps, F>: for<'c_rcv> From<&'c_rcv mut F>,
C: UniformObjectDeref<Declarer>,
pub fn connect_self<F, Declarer>(&self, function: F) -> ConnectHandlewhere
F: for<'c_rcv> SignalReceiver<&'c_rcv mut C, Ps> + 'static,
IndirectSignalReceiver<'c_rcv, &'c_rcv mut C, Ps, F>: for<'c_rcv> From<&'c_rcv mut F>,
C: UniformObjectDeref<Declarer>,
Connect a method (member function) with &mut self
as the first parameter.
- To connect to methods on other objects, use
connect_other()
. - If you need
connect flags
or cross-thread signals, usebuilder()
.
pub fn connect_other<F, OtherC, Declarer>(
&self,
object: &impl ToSignalObj<OtherC>,
method: F,
) -> ConnectHandlewhere
OtherC: UniformObjectDeref<Declarer>,
F: for<'c_rcv> SignalReceiver<&'c_rcv mut OtherC, Ps> + 'static,
IndirectSignalReceiver<'c_rcv, &'c_rcv mut OtherC, Ps, F>: for<'c_rcv> From<&'c_rcv mut F>,
pub fn connect_other<F, OtherC, Declarer>(
&self,
object: &impl ToSignalObj<OtherC>,
method: F,
) -> ConnectHandlewhere
OtherC: UniformObjectDeref<Declarer>,
F: for<'c_rcv> SignalReceiver<&'c_rcv mut OtherC, Ps> + 'static,
IndirectSignalReceiver<'c_rcv, &'c_rcv mut OtherC, Ps, F>: for<'c_rcv> From<&'c_rcv mut F>,
Connect a method (member function) with any &mut OtherC
as the first parameter, where
OtherC
: GodotClass
(both user and engine classes are accepted).
The parameter object
can be of 2 different “categories”:
- Any
&Gd<OtherC>
(e.g.:&Gd<Node>
,&Gd<CustomUserClass>
). &OtherC
, as long asOtherC
is a user class that contains abase
field (it implements theWithBaseField
trait).
- To connect to methods on the object that owns this signal, use
connect_self()
. - If you need
connect flags
or cross-thread signals, usebuilder()
.
§impl<C, R> TypedSignal<'_, C, R>
impl<C, R> TypedSignal<'_, C, R>
pub fn to_fallible_future(&self) -> FallibleSignalFuture<R> ⓘ
pub fn to_fallible_future(&self) -> FallibleSignalFuture<R> ⓘ
Creates a fallible future for this signal.
The future will resolve the next time the signal is emitted.
See FallibleSignalFuture
for details.
pub fn to_future(&self) -> SignalFuture<R> ⓘ
pub fn to_future(&self) -> SignalFuture<R> ⓘ
Creates a future for this signal.
The future will resolve the next time the signal is emitted, but might panic if the signal object is freed.
See SignalFuture
for details.