Signal
A built-in type representing a signal of anObject.
Description
Signal is a built-inVariant type that represents a signal of anObject instance. Like allVariant types, it can be stored in variables and passed to functions. Signals allow all connectedCallables (and by extension their respective objects) to listen and react to events, without directly referencing one another. This keeps the code flexible and easier to manage. You can check whether anObject has a given signal name usingObject.has_signal().
In GDScript, signals can be declared with thesignal
keyword. In C#, you may use the[Signal]
attribute on a delegate.
signalattacked# Additional arguments may be declared.# These arguments must be passed when the signal is emitted.signalitem_dropped(item_name,amount)
[Signal]delegatevoidAttackedEventHandler();// Additional arguments may be declared.// These arguments must be passed when the signal is emitted.[Signal]delegatevoidItemDroppedEventHandler(stringitemName,intamount);
Note
There are notable differences when using this API with C#. SeeC# API differences to GDScript for more information.
Tutorials
Constructors
Signal() | |
Signal(object:Object, signal:StringName) |
Methods
disconnect(callable:Callable) | |
is_connected(callable:Callable)const | |
Operators
operator !=(right:Signal) | |
operator ==(right:Signal) |
Constructor Descriptions
Constructs an emptySignal with no object nor signal name bound.
Constructs aSignal as a copy of the givenSignal.
SignalSignal(object:Object, signal:StringName)
Creates aSignal object referencing a signal namedsignal
in the specifiedobject
.
Method Descriptions
intconnect(callable:Callable, flags:int = 0)🔗
Connects this signal to the specifiedcallable
. Optionalflags
can be also added to configure the connection's behavior (seeConnectFlags constants). You can provide additional arguments to the connectedcallable
by usingCallable.bind().
A signal can only be connected once to the sameCallable. If the signal is already connected, returns@GlobalScope.ERR_INVALID_PARAMETER and pushes an error message, unless the signal is connected withObject.CONNECT_REFERENCE_COUNTED. To prevent this, useis_connected() first to check for existing connections.
forbuttonin$Buttons.get_children():button.pressed.connect(_on_pressed.bind(button))func_on_pressed(button):print(button.name," was pressed")
voiddisconnect(callable:Callable)🔗
Disconnects this signal from the specifiedCallable. If the connection does not exist, generates an error. Useis_connected() to make sure that the connection exists.
Emits this signal. AllCallables connected to this signal will be triggered. This method supports a variable number of arguments, so parameters can be passed as a comma separated list.
Returns anArray of connections for this signal. Each connection is represented as aDictionary that contains three entries:
signal
is a reference to this signal;callable
is a reference to the connectedCallable;flags
is a combination ofConnectFlags.
StringNameget_name()const🔗
Returns the name of this signal.
Returns the object emitting this signal.
Returns the ID of the object emitting this signal (seeObject.get_instance_id()).
Returnstrue
if anyCallable is connected to this signal.
boolis_connected(callable:Callable)const🔗
Returnstrue
if the specifiedCallable is connected to this signal.
Returnstrue
if thisSignal has no object and the signal name is empty. Equivalent tosignal==Signal()
.
Operator Descriptions
booloperator !=(right:Signal)🔗
Returnstrue
if the signals do not share the same object and name.
booloperator ==(right:Signal)🔗
Returnstrue
if both signals share the same object and name.