Skip to main content

05 Priority: The "Combo" Execution Order

📍 Demo Info

  • Scene Preview

    Demo 05 Scene View

  • Scene Path

    Assets/TinyGiants/GameEventSystem/Demo/05_PriorityEvent/05_PriorityEvent.unity
    Goal

    To demonstrate how the Execution Order of listeners affects gameplay logic. You will see how dragging and dropping actions in the Inspector can change a "Normal Hit" into a "Critical Combo" without changing code.


📝 Description

The "Buff-Then-Attack" Problem In game logic, sequence matters. If you have an event OnHit that triggers two actions:

  1. ActivateBuff: Turns on a "Double Damage" flag.
  2. ResolveHit: Deals damage based on the flag.
  • If (1) happens before (2) -> You get Double Damage (Success ✅).
  • If (2) happens before (1) -> You get Normal Damage (Fail ❌).

This demo uses two events to show both scenarios side-by-side.


🛠️ Scene Setup

  1. The Turret: The attacker. It has a cannon and a glowing "Core" renderer.
  2. The Target: A capsule dummy.
  3. Two Events:
    • OnOrderedHit: Configured correctly.
    • OnChaoticHit: Configured incorrectly.

🎮 How to Test

  1. Enter Play Mode.
  2. Click "Raise (Ordered Hit)" (Right Button):
    • Observation:
      1. Turret turns Gold (Buff Applied).
      2. Projectile hits.
      3. Result: CRITICAL EXPLOSION + Screen Shake + Big Damage Text.
    • Why? The Buff activated before the damage was calculated.
  3. Click "Raise (Chaotic Hit)" (Left Button):
    • Observation:
      1. Projectile hits (Weak Smoke).
      2. Turret turns Gold (Buff Applied after impact).
      3. Result: Weak Hit + Small Text.
    • Why? The damage was calculated before the buff was active.

🔑 Key Configuration

1. Event Definition (Editor)

We use the standard Sender Type <GameObject, DamageInfo> for context.

Editor List View

2. Receiver Action Binding (Behavior)

This is the most critical part. Open the Behavior Window for both events and compare them.

Correct Configuration (OnOrderedHit):

  • ActivateBuff is at the Top.
  • ResolveHit is at the Bottom.
  • Flow: Buff -> Damage.

Ordered Behavior

Incorrect Configuration (OnChaoticHit):

  • ResolveHit is at the Top.
  • ActivateBuff is at the Bottom.
  • Flow: Damage -> Buff.

Chaotic Behavior

Drag & Drop

You can reorder these actions simply by dragging the handle (=) on the left side of the list. No code changes required!

3. Raiser Event Assignment (Inspector)

The turret script holds references to both event channels.

Raiser Inspector


💻 Code Walkthrough

1. The Sender (PriorityEventRaiser.cs)

The sender just fires the event. It doesn't know about the order.

public class PriorityEventRaiser : MonoBehaviour
{
// Channel A: Configured Correctly
[GameEventDropdown] public GameEvent<GameObject, DamageInfo> orderedHitEvent;

// Channel B: Configured Incorrectly
[GameEventDropdown] public GameEvent<GameObject, DamageInfo> chaoticHitEvent;

public void FireOrderedSequence()
{
// ... Fire logic ...
orderedHitEvent.Raise(this.gameObject, info);
}
}

2. The Receiver (PriorityEventReceiver.cs)

The receiver logic depends entirely on the state of _isBuffActive.

public class PriorityEventReceiver : MonoBehaviour
{
private bool _isBuffActive;

// Action A: Sets the flag
public void ActivateBuff(GameObject sender, DamageInfo args)
{
_isBuffActive = true; // <--- The critical state change
// Play Gold Aura VFX...
Debug.Log("(A) BUFF ACTIVATED!");
}

// Action B: Reads the flag
public void ResolveHit(GameObject sender, DamageInfo args)
{
// Check state AT THE MOMENT of execution
if (_isBuffActive)
{
// CRITICAL HIT Logic
DealMassiveDamage();
}
else
{
// NORMAL HIT Logic
DealWeakDamage();
}

StartCoroutine(ResetVisuals());
}
}