---
title: "Add custom value tracks to a replay viewer module in C#"
url: https://recursiveai.org/onescope/cookbook/write-replay-module-entity-tracks/
description: "Declare a custom numeric track in a C# OneScope replay module, record an entity’s value each simulation step, and read the samples from replay history."
version: "1.0.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneScope.Sdk) · [GitHub](https://github.com/recursive-ai-oss/onescope-csharp) · [Docs](https://recursiveai.org/onescope/docs/) · [Download example (.zip)](https://recursiveai.org/onescope/cookbook/write-replay-module-entity-tracks/write-replay-module-entity-tracks.zip)

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

```shell
dotnet add package RecursiveAI.OneScope.Sdk
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
// 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](https://recursiveai.org/onescope/cookbook/write-replay-module-entity-tracks/write-replay-module-entity-tracks.zip) 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:

```csharp
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:

```csharp
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);
}
```

```csharp
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`:

```csharp
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:

```csharp
sink.Sample("counter.1.squared", (double)Count * Count);
```

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

## Related recipes

- [Write a custom replay viewer module](https://recursiveai.org/onescope/cookbook/write-a-onescope-module/)
- [Record timeline events](https://recursiveai.org/onescope/cookbook/record-custom-replay-timeline-events/)
- [Export simulation telemetry to CSV](https://recursiveai.org/onescope/cookbook/export-replay-track-samples/)

## 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.
