π¨ SS14 Events Guide
Events are the primary way of communication between systems and entities in Space Station 14. π This guide covers how to properly define, raise, and handle events while following engine standards.
π Event Definition
Local Events π
For local events (within a single client or server), use a simple struct or class structure.
- Structs: Preferred for high frequency events (e.g.
MoveEvent,DamageEvent) to avoid GC load. ποΈ - Classes: Use for complex data or events that require inheritance (e.g.
ExamineEvent). π - Naming: The
Eventsuffix is required (for example,DoorOpenedEvent).
// Simple event structure
public readonly record struct DoorOpenedEvent(EntityUid User);
// Event class with output data
public sealed class ExamineEvent : EntityEventArgs {
public readonly EntityUid Examined;
public FormattedMessage Message = new();
}
Network Events π
Events transmitted over the network MUST inherit EntityEventArgs and be marked with [Serializable, NetSerializable] attributes.
[Serializable, NetSerializable]
public sealed class RequestStationNameEvent : EntityEventArgs {
public string NewName;
}
π Subscribe to Events
Subscriptions are always processed in EntitySystem.Initialize().
1. Directed Subscription (`SubscribeLocalEvent`) π―
Use when you want to listen to an event on a specific entity that has a specific component.
Modern format: Use the Entity<T> wrapper to access the component and UID at the same time.
public override void Initialize() {
base.Initialize();
SubscribeLocalEvent<DoorComponent, DoorOpenedEvent>(OnDoorOpened);
}
private void OnDoorOpened(Entity<DoorComponent> ent, ref DoorOpenedEvent args) {
// ent.Owner is the EntityUid
// ent.Comp is a DoorComponent
if (ent.Comp.IsOpen) ...
}
2. Broadcast Subscription (`SubscribeEvent`) π’
Use for global events that are not tied to a specific entity.
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundRestart);
3. Network Subscription (`SubscribeNetworkEvent`) π‘
Use to process events sent from the other side (Client -> Server or Server -> Client).
SubscribeNetworkEvent<RequestStationNameEvent>(OnNameRequest);
π§© Specific Patterns
1. Cancellable Events π«
Used to check whether an action can be performed ("Attempt" events). Any subscriber can cancel the action.
- Classes: Inherit from
CancellableEntityEventArgs. - Structures: Add the
public bool Cancelled;field. - Important: Always pass such events through
refso that changes toCancelledare visible to the calling code.
Usage:
// Definition
public sealed class DisarmAttemptEvent : CancellableEntityEventArgs { }
// Subscription (Lock action)
private void OnDisarmAttempt(Entity<ScpRestrictionComponent> ent, ref DisarmAttemptEvent args) {
if (!ent.Comp.CanBeDisarmed)
args.Cancel(); // Or args.Cancelled = true;
}
// Call (Permission check)
var attempt = new DisarmAttemptEvent();
RaiseLocalEvent(target, attempt);
if (attempt.Cancelled)
return; // Action interrupted
2. Handled Events β
Used when an event must be processed by only one system (for example, interaction with an object). If one system has "handled" an event, the others do not need to execute their logic.
- Implementation: Add field
public bool Handled;(or inheritHandledEntityEventArgsfor classes).
Usage:
// Definition
[ByRefEvent]
public struct InteractEvent {
public bool Handled;
}
// Subscription
private void OnInteract(Entity<MyComponent> ent, ref InteractEvent args) {
if (args.Handled) return; // Already processed by someone
// Executing the logic
args.Handled = true; // Mark as processed
}
Important: The pattern of Handled is different from Cancelled. Cancelled asks for permission (βIs it possible?β), and Handled speaks of the fact of accomplishment (βI did it!β).
β‘ Performance: By-Ref Events
For high-load code, especially frequently triggered events (physics, motion), use By-Ref (reference) events. This avoids copying large structures.
Definition of By-Ref Events
Mark the structure with the [ByRefEvent] attribute.
[ByRefEvent]
public struct MoveEvent {
public EntityCoordinates OldPosition;
public EntityCoordinates NewPosition;
}
Subscription By-Ref
You MUST use the ref keyword in the handler signature. β οΈ
SubscribeLocalEvent<PhysicsComponent, MoveEvent>(OnMove);
private void OnMove(Entity<PhysicsComponent> ent, ref MoveEvent args) {
// args is passed by reference, changes are visible everywhere
}
π€ Calling Events
Calling Local Events
Use RaiseLocalEvent from EntitySystem.
// By Value
RaiseLocalEvent(uid, new DoorOpenedEvent(user));
// By reference (By Ref) - automatically for those marked [ByRefEvent]
var moveEv = new MoveEvent(oldPos, newPos);
RaiseLocalEvent(uid, ref moveEv);
β Antipatterns and Frequent Errors
1. π« Deprecated handler signature
Error: Use expanded signature (EntityUid uid, Component comp, args).
Why: This is an outdated style. The new style with Entity<T> is cleaner and more convenient.
Right:
// β
GOOD
private void OnEvent(Entity<MyComponent> ent, ref MyEvent args) { ... }
// β BAD
private void OnEvent(EntityUid uid, MyComponent component, MyEvent args) { ... }
2. π« Subscribe to `OnMapInit` or `Startup`
Error: Subscribe to events inside component lifecycle methods.
Why: This causes memory leaks and duplicate subscriptions.
Correct: Always subscribe only to Initialize() of your EntitySystem.
3. π« Using `CancellableEntityEventArgs` for Structs
Error: Trying to inherit structures from classes or using CancellableEntityEventArgs unnecessarily.
Why: This creates unnecessary allocations (boxing).
Correct: Add the bool Handled or bool Cancelled field directly to the structure and pass it through ref.
4. π« Heavy logic in event constructors
Bug: Perform complex calculations in event constructor.
Why: Events are created frequently.
Correct: Transfer only ready data.
5. π« Forgotten `sealed` for event classes
Error: Creating an event class without sealed.
Why: Prevents the JIT compiler from devirtualizing calls, reducing performance.
Correct: Always write public sealed class MyEvent.
6. π« Changing `ref` arguments unnecessarily
Bug: Change fields in ref event if you are not the "responsible" system.
Why: This may break the logic of other systems that receive the modified event.
Correct: Change the data only if your system needs to intercept or modify the result (for example, armor reduces damage).
Performance addition: `ByRef record struct`
For frequent local events, prefer this format:
[ByRefEvent] public record struct ChargedMachineActivatedEvent;
private void RaiseActivated(EntityUid uid)
{
var ev = new ChargedMachineActivatedEvent();
RaiseLocalEvent(uid, ref ev); // Important: ref is required.
}
Why is this useful?
- Less copying of events in mass flows.
- More stable behavior in hot-path compared to heavy event classes.
Anti-pattern
// β Frequent event as a class + call without by-ref:
public sealed class FrequentEvent : EntityEventArgs { }
RaiseLocalEvent(uid, new FrequentEvent());
Use classes where it is really needed due to semantics, and not out of habit.