Signal

A built-in type representing a signal of an Object.

Description

Signal is a built-in Variant type that represents a signal of an Object instance. Like all Variant types, it can be stored in variables and passed to functions. Signals allow all connected Callables (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.

In GDScript, signals can be declared with the signal keyword. In C#, you may use the [Signal] attribute on a delegate.

GDScriptC#

  1. signal attacked
  2. # Additional arguments may be declared.
  3. # These arguments must be passed when the signal is emitted.
  4. signal item_dropped(item_name, amount)
  1. [Signal]
  2. delegate void AttackedEventHandler();
  3. // Additional arguments may be declared.
  4. // These arguments must be passed when the signal is emitted.
  5. [Signal]
  6. delegate void ItemDroppedEventHandler(string itemName, int amount);

Note

There are notable differences when using this API with C#. See C# API differences to GDScript for more information.

Tutorials

Constructors

Signal

Signal()

Signal

Signal(from: Signal)

Signal

Signal(object: Object, signal: StringName)

Methods

int

connect(callable: Callable, flags: int = 0)

void

disconnect(callable: Callable)

void

emit(…) vararg const

Array

get_connections() const

StringName

get_name() const

Object

get_object() const

int

get_object_id() const

bool

is_connected(callable: Callable) const

bool

is_null() const

Operators

bool

operator !=(right: Signal)

bool

operator ==(right: Signal)


Constructor Descriptions

Signal Signal() 🔗

Constructs an empty Signal with no object nor signal name bound.


Signal Signal(from: Signal)

Constructs a Signal as a copy of the given Signal.


Signal Signal(object: Object, signal: StringName)

Creates a new Signal named signal in the specified object.


Method Descriptions

int connect(callable: Callable, flags: int = 0) 🔗

Connects this signal to the specified callable. Optional flags can be also added to configure the connection’s behavior (see ConnectFlags constants). You can provide additional arguments to the connected callable by using Callable.bind.

A signal can only be connected once to the same Callable. If the signal is already connected, returns @GlobalScope.ERR_INVALID_PARAMETER and pushes an error message, unless the signal is connected with Object.CONNECT_REFERENCE_COUNTED. To prevent this, use is_connected first to check for existing connections.

  1. for button in $Buttons.get_children():
  2. button.pressed.connect(_on_pressed.bind(button))
  3. func _on_pressed(button):
  4. print(button.name, " was pressed")

void disconnect(callable: Callable) 🔗

Disconnects this signal from the specified Callable. If the connection does not exist, generates an error. Use is_connected to make sure that the connection exists.


void emit(…) vararg const 🔗

Emits this signal. All Callables 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.


Array get_connections() const 🔗

Returns an Array of connections for this signal. Each connection is represented as a Dictionary that contains three entries:

  • signal is a reference to this signal;

  • callable is a reference to the connected Callable;

  • flags is a combination of ConnectFlags.


StringName get_name() const 🔗

Returns the name of this signal.


Object get_object() const 🔗

Returns the object emitting this signal.


int get_object_id() const 🔗

Returns the ID of the object emitting this signal (see Object.get_instance_id).


bool is_connected(callable: Callable) const 🔗

Returns true if the specified Callable is connected to this signal.


bool is_null() const 🔗

Returns true if the signal’s name does not exist in its object, or the object is not valid.


Operator Descriptions

bool operator !=(right: Signal) 🔗

Returns true if the signals do not share the same object and name.


bool operator ==(right: Signal) 🔗

Returns true if both signals share the same object and name.


User-contributed notes

Please read the User-contributed notes policy before submitting a comment.

Previous Next