Ss14 eventbus

From space-sunrise/sunrise-station · View interactive version

Ss14 eventbus is an AI agent skill from the GitHub repository space-sunrise/sunrise-station. Architectural guide to EventBus in Space Station 14 - strict event taxonomy, subscription storage, dispatch logic and internal optimization mechanisms.

How to use this skill

Skill file

๐ŸšŒ EventBus architecture in SS14

EventBus is the central nervous system of the Space Station 14 (RobustToolbox) engine. It orchestrates communication between systems and entities, implementing a highly optimized dispatch mechanism without unnecessary allocations. ๐Ÿง 

๐Ÿ—๏ธ Basic Architecture

EventBus is implemented through the IEventBus interface and combines two different event paradigms:

  1. Broadcast Events: Global events sent to all subscribers of a certain type. ๐ŸŒ
  2. Directed Events: Events targeting a specific entity are sent only to components on that entity. ๐ŸŽฏ

๐Ÿ’พ Internal Storage

To achieve high performance, EventBus supports several specialized data structures:

๐ŸŽญ Event Paradigms

1. Broadcast Events (`RaiseEvent`) ๐Ÿ“ข

2. Directed Events (`RaiseLocalEvent`) ๐ŸŽฏ

1. Finds EventTable for the target entity.

2. Finds a list of component indices (CompIdx) subscribed to this event type.

3. Gets the actual component instances from EntityManager.

4. Calls the cached handler delegate for each component.

3. Component Events (`RaiseComponentEvent`) ๐Ÿงฉ

๐Ÿ”— Mechanics of Subscriptions

Registration ๐Ÿ“

Subscriptions are usually registered during EntitySystem.Initialize():

  1. Directed: SubscribeLocalEvent<TComp, TEvent>(Handler) -> Registers Handler in _eventSubs for TComp.
  2. Broadcast: SubscribeEvent<TEvent>(Handler) -> Registers Handler to _eventData.

###Ordering ๐Ÿ”ข

Events/Subscriptions support explicit ordering using the before and after types.

๐Ÿ› ๏ธ Optimization Techniques

EventBus uses aggressive optimization to support thousands of events per tick:

โ›” Critical Limitations

  1. Lock Subscriptions: You cannot subscribe/unsubscribe while dispatching events (unless you use options with Queue). The bus is blocked during iteration. ๐Ÿ”’
  2. Overhead of Generics: The bus minimizes the use of generics in hot paths to avoid JIT overhead for each event type.
  3. Thread Safety: EventBus is not thread safe. All events must be raised on the main thread. ๐Ÿงต
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.