Recursive AI

Record timeline events in a C# simulation replay

OneScope is a free, open-source viewer for simulation recordings, with a desktop window or command-line control, from Recursive AI. MIT or Apache-2.0.

Use CounterModule.cs from the zip to log replay events.

Terminal
dotnet add package RecursiveAI.OneScope.Sdk

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
// CNTR + 4 steps
byte[] data = [67, 78, 84, 82, 4, 0, 0, 0];
var history = CounterModule.Replay(data);
var item = history.Tracks[0].Samples[0];
Console.WriteLine($"{item.Step}: {item.Text}");
Text
3: Order completed

CounterModule.cs declares the event track in CounterSource.Info:

C#
Tracks = [new("orders", "Orders", TrackKind.Event)]

CounterWorld.Advance writes to that track through the sink:

C#
public void Advance(ReadOnlySpan<byte> input, TimelineSink sink)
{
    if (!input.IsEmpty) throw new ScopeFormatException("counter", "Expected empty step input.");
    Step = checked(Step + 1);
    if (Step == 3)
        sink.Event("orders", new EntityId(1), "Order completed");
}

Unzip the example, then dotnet run.

Replay writer and rewind check

The input bytes are CNTR followed by the little-endian step count 4. Program.cs writes the four-step recording to counter.replay, plays it, and then rewinds and plays it again. All four steps have empty inputs.

C#
using OneScope.Sdk;
 
// CNTR + 4 steps (little-endian).
byte[] data = [67, 78, 84, 82, 4, 0, 0, 0];
File.WriteAllBytes("counter.replay", data);
var history = CounterModule.Replay(data);
var item = history.Tracks[0].Samples[0];
Console.WriteLine($"{item.Step}: {item.Text}");
 
history.Seek(1);
history.Seek(4);
Console.WriteLine($"Events after rewind: {history.Tracks[0].Samples.Count}");
Text
3: Order completed
Events after rewind: 1

The history’s sink throws ArgumentException if an event uses an undeclared track ID or a track declared with the wrong kind.

Variations

To see the event in the timeline, install the viewer too:

Terminal
dotnet tool install --global RecursiveAI.OneScope.Tool

After dotnet run writes counter.replay, open it from the example folder. The Orders row marks step 3.

Terminal
onescope open counter.replay \
  --module bin/Debug/net10.0/Example.dll

The short answer

Declare a TrackInfo with TrackKind.Event and give it a stable ID. When IScopeWorld.Advance reaches a step that produces an event, pass that ID, the entity, and a short label to TimelineSink.Event. The source’s track declaration gives the viewer its row; the call adds a sample at the step just reached. Use different track IDs for events you want on different rows, not for each occurrence of the same event.

How do you add custom events to a simulation replay?

Decide which transitions the reader should be able to locate. For a job queue, “Order completed” is a transition; “Order is completed” is state that could stay true for hundreds of steps. Emit the former when the transition happens. Inspectable properties can still show the latter whenever someone selects the entity. If the event concerns the world as a whole rather than one entity, use EntityId.None.

Keep the track kind matched to the information you have. An event carries an entity and a text label. A marker has a label but no entity. A series is for numeric values, such as the number of waiting orders, where you want to see how the value changes from step to step. Use a finding instead when the viewer needs a severity, a stable code, and an explanation of a possible issue.

Does seeking backward record the events again?

No. A seek can restore a snapshot and run steps again, so emitting events from Advance does not mean one new timeline entry on every visit. OneScope’s history records timeline data the first time it runs each step. The example seeks to step 4, rewinds to step 1, and goes to step 4 again; it still has one event. Do not put simulation changes behind sink.IsRecording: it can be false on the second visit, but the simulation must still reach the same state. It is useful when preparing event text is work you can skip on a repeat visit.

The event track is a view of what the replay produces, not a substitute for saving inputs. If completing the order depends on a user command, keep that command in the recording so playback reaches the same transition. See the module contract for the track types and the history guide for how the viewer reconstructs steps.

All onescope recipes