Course contentsShow
← Unity Game Development
Lesson 459 of 8287. Advanced Game ArchitectureFree lesson

Building a Global Event Bus System

Create a centralized static event bus that enables communication between systems that have no direct reference to each other.

In this lesson, you will learn how to create a centralized Global Event Bus that allows disparate game systems to communicate without holding direct references to one another.

The Problem: Tight Coupling

As your game scales, you often run into "spaghetti code" where your PlayerController needs to talk to the UIManager, the SoundManager, and the QuestTracker. If you manually link these via the Inspector or GetComponent, you create a nightmare of dependencies where changing one class breaks three others.

The Solution: The Global Event Bus

A Global Event Bus acts as a post office. Systems that need to announce something "post" an event to the bus, and systems interested in that information "subscribe" to it. Neither party knows the other exists; they only know about the bus.

We can achieve this using static C# Action delegates. Here is a simple implementation:

using System;
using System.Collections.Generic;

public static class EventBus
{
    // A dictionary to store events by their unique type or key
    public static event Action OnPlayerDeath;

    public static void PublishPlayerDeath()
    {
        OnPlayerDeath?.Invoke();
    }
}

How It Works

  1. Publishing: When the Player health reaches zero, it calls EventBus.PublishPlayerDeath(). It doesn't care who is listening.
  2. Subscribing: The UIManager subscribes to the bus in its OnEnable method (EventBus.OnPlayerDeath += ShowGameOverScreen;) and unsubscribes in OnDisable to prevent memory leaks.
  3. Decoupling: You can add a SoundManager that plays a "Sad Trombone" sound on the same event without ever modifying the Player or UIManager scripts.

By routing communication through this static intermediary, your systems remain lean and modular. You adhere to the Open-Closed Principle because you can add new listeners to an event without changing the code that triggers the event.

Key Takeaway: The Global Event Bus eliminates direct dependencies between classes by acting as a universal messenger, allowing systems to communicate solely through shared event contracts.