Event Queue
Event Queue
Section titled “Event Queue”パターン一言説明
Section titled “パターン一言説明”イベントの発行と消費をキューで分離し、システム間の結合を下げるパターンです。
Unity でよく使う状況
Section titled “Unity でよく使う状況”- 戦闘イベントを UI / サウンド / 報酬へ同時に届けたいとき
- 即時呼び出しによる密結合を減らしたいとき
構成要素(役割)
Section titled “構成要素(役割)”- Publisher: イベント発行者
- Queue: イベント保存先
- Consumer: フレーム単位の処理者
Unity 例 (C#)
Section titled “Unity 例 (C#)”以下のコードは、上で説明した状況を Unity プロジェクトの文脈で単純化した例です。
using System.Collections.Generic;using UnityEngine;
public readonly struct CombatEvent{ public readonly string EventType; public readonly int Value;
public CombatEvent(string eventType, int value) { EventType = eventType; Value = value; }}
public sealed class CombatEventQueue : MonoBehaviour{ private readonly Queue<CombatEvent> pendingEvents = new();
public void Publish(CombatEvent combatEvent) { pendingEvents.Enqueue(combatEvent); }
private void Update() { while (pendingEvents.Count > 0) { CombatEvent combatEvent = pendingEvents.Dequeue(); Debug.Log($"${combatEvent.EventType}: ${combatEvent.Value}"); } }}- 生産者と消費者のタイミングを分離でき、システム間の直接依存を減らせます。
- イベントログ、リプレイ、バッチ処理のような拡張がしやすくなります。
- キューが詰まると遅延が発生し、応答性が落ちることがあります。
- 順序や重複処理のルールが曖昧だと、再現しづらいバグにつながります。
動作ダイアグラム
Section titled “動作ダイアグラム”生産者と消費者をキューで分離して非同期に受け渡す流れです。