Signal
A built-in type representing a signal of an Object
.
描述
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 Callable
s (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 an Object
has a given signal name using Object.has_signal
.
In GDScript, signals can be declared with the signal
keyword. In C#, you may use the [Signal]
attribute on a delegate.
signal attacked
# Additional arguments may be declared.
# These arguments must be passed when the signal is emitted.
signal item_dropped(item_name, amount)
[Signal]
delegate void AttackedEventHandler();
// Additional arguments may be declared.
// These arguments must be passed when the signal is emitted.
[Signal]
delegate void ItemDroppedEventHandler(string itemName, int amount);
通过 C# 使用该 API 时会有显著不同,详见 :ref:doc_c_sharp_differences
\ 。
构造函数
方法
int | connect ( callable: Callable , flags: int = 0 ) |
void | disconnect ( callable: Callable ) |
void | emit ( ... ) vararg1 const2 |
Array | get_connections ( ) const2 |
StringName | get_name ( ) const2 |
Object | get_object ( ) const2 |
int | get_object_id ( ) const2 |
bool | has_connections ( ) const2 |
bool | is_connected ( callable: Callable ) const2 |
bool | is_null ( ) const2 |
运算符
bool | operator != ( right: Signal ) |
bool | operator == ( right: Signal ) |
构造函数说明
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 Signal object referencing a signal named signal
in the specified object
.
方法说明
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.
for button in $Buttons.get_children():
button.pressed.connect(_on_pressed.bind(button))
func _on_pressed(button):
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 ( ... ) vararg1 const2
Emits this signal. All Callable
s 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 ( ) const2
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 connectedCallable
; -
flags
is a combination of ConnectFlags.
StringName
get_name ( ) const2
Returns the name of this signal.
Returns the object emitting this signal.
Returns the ID of the object emitting this signal (see Object.get_instance_id
).
bool
has_connections ( ) const2
Returns true
if any Callable
is connected to this signal.
bool
is_connected ( callable: Callable
) const2
Returns true
if the specified Callable
is connected to this signal.
Returns true
if this Signal has no object and the signal name is empty. Equivalent to signal == Signal()
.
运算符说明
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.
本方法通常需要用户覆盖才能生效。
本方法无副作用,不会修改该实例的任何成员变量。
本方法除了能接受在此处描述的参数外,还能够继续接受任意数量的参数。
本方法用于构造某个类型。
调用本方法无需实例,可直接使用类名进行调用。
本方法描述的是使用本类型作为左操作数的有效运算符。
这个值是由下列位标志构成位掩码的整数。
无返回值。