Class GameSystem
ENGINE LAYER - Base class for all game systems with standardized lifecycle
Responsibilities:
- Define standard system lifecycle (Initialize, Shutdown, Save, Load)
- Enforce dependency validation before initialization
- Prevent re-initialization and initialization of missing dependencies
- Provide consistent logging for system operations
Architecture:
- Pure mechanism, no game-specific knowledge
- Systems declare dependencies explicitly via GetDependencies()
- Initialization order determined by dependency graph
- All state changes go through standard lifecycle methods
Usage (Game Layer): public class EconomySystem : GameSystem { public override string SystemName => "Economy";
protected override IEnumerable{GameSystem} GetDependencies()
{
yield return timeManager;
yield return provinceSystem;
}
protected override void OnInitialize()
{
// Dependencies guaranteed to be initialized here
timeManager.OnMonthlyTick += CollectTaxes;
}
}
Benefits:
- No load order bugs (dependencies validated)
- No circular dependency crashes (detected at startup)
- Easy testing (mock dependencies)
- Save/load support (standard serialization hooks)
- Self-documenting (dependencies explicit)
public abstract class GameSystem : MonoBehaviour
- Inheritance
-
objectGameSystem
- Derived
Properties
IsInitialized
Whether this system has been initialized Systems cannot be used until initialized
public bool IsInitialized { get; }
Property Value
- bool
SystemName
Unique name for this system (used in logging and debugging)
public abstract string SystemName { get; }
Property Value
- string
Methods
EnsureInitialized()
Check if this system is ready to be used Systems should check this before performing operations
protected void EnsureInitialized()
GetDependencies()
Get all systems this system depends on Dependencies must be initialized before this system Override to declare dependencies (return empty if none) Internal visibility allows SystemRegistry to access for dependency resolution
protected virtual IEnumerable<GameSystem> GetDependencies()
Returns
- IEnumerable<GameSystem>
Initialize()
Initialize this system (call after all dependencies initialized) Validates dependencies before calling OnInitialize()
public void Initialize()
LogSystem(string)
Log a system-specific message Prefixes message with system name for easier debugging
protected void LogSystem(string message)
Parameters
messagestring
LogSystemError(string)
Log a system-specific error
protected void LogSystemError(string message)
Parameters
messagestring
LogSystemWarning(string)
Log a system-specific warning
protected void LogSystemWarning(string message)
Parameters
messagestring
OnDestroy()
Unity lifecycle - automatically shutdown on destroy
protected virtual void OnDestroy()
OnInitialize()
Perform system-specific initialization All dependencies guaranteed to be initialized when this is called Override to set up system state, subscribe to events, etc.
protected abstract void OnInitialize()
OnLoad(SaveGameData)
Load system state from save data Override to deserialize system state for save/load
protected virtual void OnLoad(SaveGameData saveData)
Parameters
saveDataSaveGameData
OnSave(SaveGameData)
Save system state to save data Override to serialize system state for save/load
protected virtual void OnSave(SaveGameData saveData)
Parameters
saveDataSaveGameData
OnShutdown()
Perform system-specific shutdown Override to clean up resources, unsubscribe events, etc.
protected virtual void OnShutdown()
Shutdown()
Shutdown this system cleanly Unsubscribe from events, release resources, etc. Called before scene unload or game exit
public void Shutdown()
Subscribe<T>(EventBus, Action<T>)
Subscribe to an event using a specific EventBus. Useful when not using GameState.Instance.
protected void Subscribe<T>(EventBus eventBus, Action<T> handler) where T : struct, IGameEvent
Parameters
eventBusEventBushandlerAction<T>
Type Parameters
T
Subscribe<T>(Action<T>)
Subscribe to an event with automatic cleanup on shutdown. No need to manually unsubscribe - handled automatically.
Usage: protected override void OnInitialize() { Subscribe{MonthlyTickEvent}(HandleMonthlyTick); } // No OnShutdown override needed for unsubscription
protected void Subscribe<T>(Action<T> handler) where T : struct, IGameEvent
Parameters
handlerAction<T>
Type Parameters
T