Aego Русский

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, before Emit returns. 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) or ref.Name.Connect(Method) registers a plain method of this script, or ref.Method of another instance, and returns a Connection. 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 Emit returns; the running Emit uses 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 a nil handle it does nothing.

Recursion

A handler that emits again goes one call deeper; endless re-emitting ends with AES6003 on the emitting instance.

Cost

Emit allocates 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))
    }
}

Engine signals

Components and engine handles have signals too. Scripts connect to them and never emit them:

hp := node.Get<Health>()
conn = hp.Died.Connect(OnDied)
fade = tween.ToFloat(node, "SpriteRenderer.Tint.a", 0, 0.3, curve.Linear).Completed.Connect(OnFaded)

The same rules apply as for script signals. A component signal's connections end when its node is destroyed or the component removed; a handle signal's connections end after the handle's last emit (a tween's Completed, the last Fired of a one-shot timer). Connecting to a finished handle returns a nil Connection. Emit on an engine signal is AES2055. Parameters of engine signals are numbers, vectors, colors, enums and references, never strings or slices.

Go code subscribes with sdk.Connect1[A](ctx, node, "Health.Died", fn) or "script/Enemy.Died". A Go handler that panics is disconnected and reported; it never stops a script.