Events & tokens
AzEventToken
Manage one subscription with dispose, pause, resume, diagnostics, and equality behavior.
- Dispose
- Pause
- Resume
- Diagnostics
An AzEventToken is the lifecycle handle for one subscription.
AzEventToken
AzEventToken is the lightweight value returned by Subscribe and SubscribeUnique.
AzEventToken token = CombatEvents.HealthChanged.Subscribe(OnHealthChanged);Token API
IsValidTrue when the token has an owner.
AzEventToken.Invalidis not valid.IsDisposedTrue when the token no longer controls a live subscription.
IsPausedTrue when the token controls a live paused subscription.
Dispose()Removes the subscription. Safe to call more than once.
Pause()Temporarily disables the subscription.
Resume()Re-enables the subscription.
DiagnosticIdStable diagnostic string such as
AZE-12-3. Empty for invalid tokens.ToString()Debug-friendly state string, such as
AzEventToken[Active:AZE-12-3].
Disposing a Token
Disposing a token unsubscribes the handler represented by that token.
private AzEventToken _healthToken;
private void OnEnable()
{
_healthToken = CombatEvents.HealthChanged.Subscribe(OnHealthChanged);
}
private void OnDisable()
{
_healthToken.Dispose();
_healthToken = AzEventToken.Invalid;
}Calling Dispose() on an already disposed token is harmless.
Pausing and Resuming a Token
Pausing keeps the subscription slot but prevents the handler from being called.
_healthToken.Pause();
// Handler will not run while paused.
CombatEvents.HealthChanged.Invoke(10);
_healthToken.Resume();
// Handler can run again.
CombatEvents.HealthChanged.Invoke(20);Use pause/resume when:
- a pooled object is temporarily inactive
- a component should stop reacting while a menu or cutscene is active
- you want to avoid unsubscribe/resubscribe churn
- you need the subscription to keep its relative order
Use dispose when:
- the owning object is being destroyed
- the subscription should never be used again
- you want the event to release that subscriber completely
Invalid Tokens
AzEventToken.Invalid means "no subscription."
You normally see it when SubscribeUnique refuses a duplicate:
AzEventToken token = CombatEvents.HealthChanged.SubscribeUnique(OnHealthChanged);
if (!token.IsValid)
{
return;
}Adding an invalid or disposed token to an AzEventTokenBag is ignored.
Token Equality
Tokens compare by owner, slot, and generation. Two tokens are equal only when they represent the same subscription instance.
AzEventToken a = evt.Subscribe(Handler);
AzEventToken b = a;
Debug.Log(a == b); // trueIf a subscription is disposed and a later subscription reuses the same internal slot, the generation changes. The old token will not control the new subscription.
Diagnostic IDs
Use DiagnosticId when logging subscription lifecycle issues:
AzEventToken token = CombatEvents.HealthChanged.Subscribe(OnHealthChanged);
Debug.Log($"Subscribed {token.DiagnosticId}");Prefix meanings:
AZEAzEvent,AzEvent<T>, orAzEventRef<T>AZPAzPriorityEventfamilyAZSAzStatesubscriptionsNETnetwork event operations
For this guide, AZE is the one you will usually see.
