Signals
script EnemyAI {
signal Died()
func Kill() {
Died.Emit()
node.Destroy()
}
}
script PlayerController {
@export var Enemy NodeRef
var kills int
var conn Connection
func Ready() {
e := Enemy.GetScript<EnemyAI>()
if e != nil {
conn = e.Died.Connect(OnEnemyDied)
}
}
func OnEnemyDied() {
kills++
}
}
- Declaration.
signal Name(params)inside a script. Signals have no result. An uppercase name is visible from other packages. - Emit.
Name.Emit(args)is allowed only inside the script that declares the signal (AES2055). Handlers run synchronously, in connection order, beforeEmitreturns. Each handler has its own instruction budget; a fault in a handler stops the handler's owner, not the emitter, and the remaining handlers still run. - Connect.
Name.Connect(Method)orref.Name.Connect(Method)registers a plain method of this script, orref.Methodof another instance, and returns aConnection. The handler must take exactly the signal's parameter types and return nothing (AES2054). Coroutines and lifecycle methods cannot be handlers (AES2055). Connecting the same method twice calls it twice. - Changes during Emit. Connections added or removed by a handler take effect after the current
Emitreturns; the runningEmituses the list it started with. - Lifetime. When the source instance stops, all its connections are removed. When a handler's owner stops, its connections are skipped and removed.
Connected()becomes false in both cases.c.Disconnect()removes one connection; on anilhandle it does nothing. - Recursion. A handler that emits again goes one call deeper; endless re-emitting ends with
AES6003on the emitting instance. - Cost.
Emitallocates nothing; a scene with ten thousand connections and a thousand emits per frame produces no garbage.
Run it:
package demo
import "aego/log"
script Counter {
signal Ticked(n int)
var frames int
var conn Connection
func Ready() {
conn = Ticked.Connect(OnTicked)
}
func Update(dt float) {
frames++
if frames%30 == 0 {
Ticked.Emit(frames)
}
}
func OnTicked(n int) {
log.Info("tick at frame " + string(n))
}
}