Recursive AI

Add custom value tracks to a replay viewer module in C#

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.

Declare a series track to plot a number over time. Call Sample after each step.

Terminal
dotnet add package RecursiveAI.OneScope.Sdk

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

C#
// In your source's SourceInfo:
Tracks = [new("counter.1.count",
    "Counter 1 count", TrackKind.Series)],
 
// In your world's Advance, after updating Count:
sink.Sample("counter.1.count", Count);

The example is the full program below: unzip it and dotnet run.

Start with a module that implements IScopeSource and IScopeWorld, or run the example as-is. The source describes the recording; the world runs its simulation steps. The example records the value of counter 1 and prints the three samples.

The full program

The zip includes CounterModule.cs, which implements the two edits above using a shared TrackId constant. The input COUNT 3 is inline: it means three steps with no per-step input. The counter adds two each step.

CounterSource.Info declares the track:

C#
public SourceInfo Info { get; } = new()
{
    ModuleId = "replay-counter", Name = name, Dimensions = 2,
    Unit = LengthUnit.Metre,
    Axes = [new(AxisIds.Time, "Time", 0)],
    EntityKinds = [new("counter", "Counter")],
    Overlays = [new(0, "Counter", true)],
    Tracks = [CounterWorld.CounterTrack],
};

CounterWorld.Advance writes the sample:

C#
public const string TrackId = "counter.1.count";
public static TrackInfo CounterTrack => new(TrackId, "Counter 1 count", TrackKind.Series);
 
public long Step { get; private set; }
public long Count { get; private set; }
public ulong Hash() => (ulong)Count;
 
public void Advance(ReadOnlySpan<byte> input, TimelineSink sink)
{
    Step++;
    Count += 2;
    sink.Sample(TrackId, Count);
}
C#
using OneScope.Sdk;
 
var source = new CounterModule().Open(
    "COUNT 3"u8.ToArray(), "counter");
var history = new History(source);
history.Scan();
var track = history.Tracks.Single();
foreach (var sample in track.Samples)
    Console.WriteLine(
        $"Step {sample.Step}: {sample.Value}");
Text
Step 1: 2
Step 2: 4
Step 3: 6

While recording through History, a sample with an undeclared ID or the wrong track kind throws ArgumentException.

Variations

To plot another computed value, add a second declaration to the Tracks array in CounterSource.Info:

C#
new("counter.1.squared", "Count squared",
    TrackKind.Series),

Sample it in Advance alongside the original count, and replace history.Tracks.Single() with history.Tracks.Single(t => t.Info.Id == "counter.1.squared") in the runner:

C#
sink.Sample("counter.1.squared", (double)Count * Count);

The values at steps 1–3 are 4, 16, and 36.

The short answer

Declare each numeric series with a TrackInfo entry in SourceInfo.Tracks. In IScopeWorld.Advance, call TimelineSink.Sample with that ID and the value from the step you just reached. For an entity-specific line, put the entity’s stable ID in the track ID and give it a readable display name. A series sample carries a number, not an entity reference; the association comes from the declaration. If you need an event tied to an entity, use an event track, whose samples can carry an EntityId and text.

How do I record custom simulation data in a replay viewer?

Choose what you want to compare before choosing a track. A series is for a number such as speed or constraint error. A marker labels a step, such as a phase change. An event records something that happened at that step, such as a contact beginning. Declare each track once per source. The ID is what your code passes to the sink; the name is what a reader uses to tell the lines apart. For two counters, declare two distinct IDs instead of writing two values to one series on the same step. The SDK expects at most one value per track per step.

Put the sample after applying the recorded input and stepping your world, so the value describes the step shown on the timeline. Calculate it from that world state, not from how often the window repaints. In the example, sampling before incrementing the count would plot 0, 2, and 4 instead of 2, 4, and 6.

Does rewinding a replay duplicate the samples?

No. The history sink records a step’s samples the first time that step runs and ignores them on later runs of the same step. Your module can call Sample unconditionally. It doesn’t need a separate list of steps it has already visited. To collect the whole series without using the desktop viewer, call History.Scan() before reading History.Tracks, as the example does. If you’re exporting samples, check the track’s Truncated flag before treating it as a complete series.

Keep the track separate from your replay’s state. It describes what happened; it isn’t a substitute for the inputs used to reconstruct the simulation or the snapshot used to seek within it. Restore the state needed to compute the same value again, then let the history decide whether to keep the sample.

All onescope recipes