Game Events
In short
GameEventsis a static class of C# events with oneRaiseXmethod each. Gameplay raises; UI, audio and effects subscribe inOnEnableand unsubscribe inOnDisable. Add a reaction by subscribing, never by editing the code that raises.
How it works
- A system calls
GameEvents.RaiseX(payload). - Every subscriber runs at once, on the main thread, in subscription order.
- On boot
GameBootstrap.ResetStaticscallsGameEvents.ClearAll(), so Play mode without a domain reload never keeps old subscribers.
The at-bat raises the same events when it replays another player’s at-bat in co-op (AtBatController.Spectate), so the HUD, crowd, sounds and scoreboard react to a teammate’s swing exactly as to your own (Live Spectating).
Every event
| Event | Payload | Raised by | Listened to by |
|---|---|---|---|
StateChanged | GameState | GameRoot.SetState | GameAudio (organ on menus, off at bat, in CoopWatch and in the Intro / Outro cutscenes), AtBatController, BattingCage (clears IsActive), ScreenFade, CustomizeView.OnStateChanged (via UIRoot) |
PitchThrown | PitchData | AtBatController.PitchRoutine, AtBatController.SpectatePitch | GameAudio (whir, glove pop, special-ball cue), TimingIndicator, HudView, FlyingHat (hat flies back on), CoopRun.OnMyPitch (during your turn), SwingLog (remembers the count before the pitch, #47), AutoPilot (decides its swing, Sim Lab) |
SwingStarted | none | AtBatController (swing press), spectate replay | GameAudio (whoosh), CoopRun.OnMySwing (during your turn), SwingLog (captures SwingInput.PressTime against the pitch’s arrival and the bat aim at the press, #47) |
SwingResolved | SwingResult | AtBatController, spectate replay | GameAudio (crack, ping, green cheer, ooh), HudView, GreenFeedback (camera shake), StadiumCrowd |
HitLanded | HitOutcome | AtBatController.ResolvePlay (when the ball first lands) | GameAudio, HudView, Scoreboard (HOME RUN! flash), StadiumCrowd, BeanBatter and HumanBatter (knock the hat off on a long home run) |
PlayEnded | PlayReport (headline, color, pitch, swing, hit) | AtBatController.ReportPlay | HudView, CoopRun.OnMyPlay (sends the play feed), SwingLog (one line in swings.csv, Swing Log) |
CountChanged | CountState (balls, strikes, outs, outsAllowed) | CountTracker.Notify | GameAudio (wood block, strikeout groan), SwingLog (the strikes column), AutoPilot (the count its bot decides on) |
Feedback | string, Color | AtBatController (“Now facing …” except on the walk-up after the match intro, “INNING n CLEARED!”, “ROUND CLEARED!”, “TURN OVER”, “Out of outs…”, ”$$$ BALL! +n COINS”), spectate replay (“Now facing …”), CoopRun (“INNING n CLEARED!”, “OUT OF OUTS…“) | HudView |
RoundFinished | RoundResult | AtBatController when a round or co-op turn ends | GameAudio (cheer or groan), HudView, AutoPilot (one inning line in soak-log.txt) |
RunEnded | RunSummary | RunManager.EndRun, CoopRun.EndRun, both from ShowResults (after the MAP BEATEN / GAME OVER cutscene when one plays) | GameAudio (win cheer, new-best chime), AutoPilot (one run line in soak-log.txt) |
CosmeticsChanged | none | MetaProgression.Equip, MetaProgression.Unequip | AtBatController.RefreshCosmetics |
ScoreboardHit | HitOutcome | AtBatController.BallInPlay (when the ball reaches the board), ScoreboardSmashTest.SmashNow (Editor) | Scoreboard (smash), GameAudio (smash sound) |
Numbers
| What | Value | Where to tune |
|---|---|---|
| Events on the bus | 12 | Scripts/Core/GameEvents.cs |
In the code
| File | What it does |
|---|---|
Scripts/Core/GameEvents.cs | The events, RaiseX methods and ClearAll. |
Scripts/Batting/AtBatController.cs | Raises most gameplay events. |
Scripts/Batting/CountTracker.cs | Raises CountChanged. |
Scripts/Audio/GameAudio.cs | The biggest listener: every sound comes from an event. |
Scripts/UI/HudView.cs | Subscribe / Unsubscribe called by UIRoot.OnEnable / OnDisable. |
Gotchas
- Adding an event means three edits in
GameEvents.cs: theevent, theRaiseXmethod and a line inClearAll. ForgettingClearAllleaks subscribers between Play sessions with domain reload off. - Not everything is on the bus.
AtBatController.BeginRoundtakes a finish callback,AtBatController.SwingCompletedis its own event (used byCoopRun), andNetRuntime/NetSessionhave their own events (Lobby and Session). HitLandedfires when the ball first touches down, not when it stops rolling.ScoreboardHitfires during the flight, when the playback reaches the smash (or at once if the player skips).CoopRunonly listens toPlayEnded,PitchThrownandSwingStartedwhile it is your turn (UnhookMyTurnremoves them).- No event was added between e7dba51 and b849a3a (#47 only added listeners:
SwingLogon three more events,AutoPiloton four). The cutscenes and the hit show are driven directly, not through the bus:MatchIntroDirector,OutroDirector,FireworksandHitCountercallGameAudiostatics (Whoosh,Impact,Fanfare,FireworkBang,CounterTick,Gone…) and theStadiumCrowd/Scoreboardmethods (Celebrate,Slump,ShowMessage) themselves (Cutscenes, Hit Show). RunEndednow fires after the end-of-game cutscene, but the run is recorded and saved before it starts, so quitting mid-cutscene loses nothing.SwingLogskips spectated pitches: in co-op they are logged on the batter’s own machine. It also skips every pitch whileSwingInput.Botis set (the dev bot batting).- The sim (
SimInning,SimCount) raises nothing on the bus: that is how thousands of runs play with no sound or HUD (Sim Lab).
Related
- Architecture: why systems talk through events
- Audio: what each event sounds like
- Add a Sound: hooking a new sound to an event
- At-Bat Loop: when in a pitch each event fires
- Scoreboard: the
ScoreboardHitflow - Swing Log: the
PlayEndedlistener that writesswings.csv