Skip to main content

The Event System

Now we're getting to our first library, ETS2LA.Backend.Events.

Most of inter-plugin communication is done via the ETS2LA EventBus. This bus provides an interface for plugins to listen to, and emit events between each other. We're also passing in events from the game into this bus, so that the same API can be used for everything.

How does the events system work?

using ETS2LA.Backend.Events;

// Provider will call .Publish to fire an event to all subscribers.
// You have to include the type of data you're sending as a generic parameter.
Events.Current.Publish<float>("ExampleProvider.CurMicroseconds", System.DateTime.Now.Microsecond);

// And now a subscriber can listen to that event via a callback function.
Events.Current.Subscribe<float>("ExampleProvider.CurMicroseconds", OnCurMicroseconds);

void OnCurMicroseconds(float microseconds)
{
Console.WriteLine($"Current microseconds: {microseconds}");
}

And that's pretty much it. Those two commands are everything you need to get access to all the game telemetry data as well as data from 3rd party plugins. I've compiled a list below of the most commonly used events, but keep in mind that at least ETS2LA's official events have a typed way to get their ID as shown below.

  • Telemetry Events : (ETS2LA.Telemetry.) GameTelemetry.Current.EventString
    • Publishes: GameTelemetryData, check it's page for more information.
note

Did you notice the mistake in that code snippet? The event got .Published before a subscriber was registered, so the subscriber never actually received the event! ETS2LA does not hold the latest state of events in memory, so you have to make sure you take this into account.