Skip to main content

Set Listener Order for a GES Event

TinyGiants
Unity Games & Tools Developer

If a damage event changes health and refreshes the HUD, register both listeners in the same priority tier and give the data update higher priority. Use the listener execution stages to check the order when the event mixes Inspector actions with C# callbacks.

Set priorities within one stage​

Register the health update and the HUD refresh as Priority listeners. Give the health update the higher number. Each callback receives the original event argument, so apply damage reduction inside the callback that writes health.

using TinyGiants.GES.Runtime;
using UnityEngine;

public sealed class DamageListeners : MonoBehaviour
{
[GameEventDropdown, SerializeField] private Int32GameEvent onPlayerDamaged;
[SerializeField] private int armor;
[SerializeField] private int health = 100;

private void OnEnable()
{
onPlayerDamaged.AddPriorityListener(ApplyDamage, priority: 100);
onPlayerDamaged.AddPriorityListener(RefreshHealthUI, priority: 25);
}

private void OnDisable()
{
onPlayerDamaged.RemovePriorityListener(ApplyDamage);
onPlayerDamaged.RemovePriorityListener(RefreshHealthUI);
}

private void ApplyDamage(int amount)
{
health = Mathf.Max(0, health - Mathf.Max(0, amount - armor));
}

private void RefreshHealthUI(int amount)
{
Debug.Log($"Health after {amount} raw damage: {health}");
}
}

The ordered Demo gives the state update and presentation callbacks different priorities. In the first run, the HUD reads the updated health.

Priority Behavior Ordered

The chaotic run shows what stale UI looks like when the display reads health before the write.

Priority Behavior Chaotic

Give dependent callbacks distinct priorities. Use names such as Model and View when several events share the same convention.

Check the dispatch order​

StageWhat runs
1Basic listeners, in registration order
2Inspector actions, when enabled
3Priority listeners, higher values first
4Conditional listeners, higher values first, with each predicate checked before its callback
5Persistent listeners, higher values first
6Trigger registrations
7Chain registrations

Priority applies within a stage. A Basic HUD callback runs before a Priority health update even if the update has priority 100. Move both callbacks to the Priority stage, or let the health update raise a separate event after writing the new value.

Open the event in Game Event Monitor and inspect Listeners to check where each callback is registered.

Monitor Listeners

Filter a callback by its argument​

Add a Conditional listener when a reaction applies only to some raises. For Int32GameEvent, the predicate receives an int.

onPlayerDamaged.AddConditionalListener(
call: HandleCriticalHit,
condition: amount => amount > 50,
priority: 75);

// When the owner stops listening:
onPlayerDamaged.RemoveConditionalListener(HandleCriticalHit);

A GameEvent<GameObject, int> passes the sender and argument to both the predicate and the callback.

onDamageFromSource.AddConditionalListener(
call: HandleBossDamage,
condition: (sender, amount) => sender != null && sender.CompareTag("Boss"),
priority: 75);

Listen across scenes​

Register the callback with AddPersistentListener when the owner is a session service that survives scene loads. In the persistent-listener Demo, the audio service stays alive and continues receiving sound requests as scenes change.

Persistent Behavior

private void Start()
{
DontDestroyOnLoad(gameObject);
onEnemyDied.AddPersistentListener(PlayDeathSound, priority: 10);
}

private void OnDestroy()
{
onEnemyDied.RemovePersistentListener(PlayDeathSound);
}

In the Demo scene setup, locate the service that owns the callback. Its lifetime determines when to remove the listener. Pair registration in OnEnable with removal in OnDisable if disabling the service should also mute it.

Persistent Scene Setup

RemoveAllListeners() clears Basic, Priority, and Conditional listeners. Remove a Persistent listener with RemovePersistentListener.

Keep the callback you registered​

Register a named method and pass the same method to the matching remove call. If you need a lambda, store it as a UnityAction<int> for an Int32GameEvent.

private UnityEngine.Events.UnityAction<int> _handler;

private void OnEnable()
{
_handler = amount => health -= amount;
onPlayerDamaged.AddListener(_handler);
}

private void OnDisable()
{
onPlayerDamaged.RemoveListener(_handler);
}

🚀 Global Developer Service Matrix

🇨🇳 China Developer Community

🌐 Global Developer Community

📧 Support & Collaboration