---
title: "Draw entity links in a C# replay"
url: https://recursiveai.org/onescope/cookbook/draw-entity-links-in-replay/
description: "Draw a line between related entities in a OneScope replay, update its endpoints as you seek, and hide it with a separate overlay toggle."
version: "1.0.0"
---

# Draw entity links in a C# 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.

[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/draw-entity-links-in-replay/draw-entity-links-in-replay.zip)

```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
using System.Numerics;
using OneScope.Sdk;
var parent = Vector3.Zero;
var child = new Vector3(3, 2, 0);
var frame = new DrawList();
var style = new DrawStyle(
    Color32.White, Overlay: 1);
frame.Line(parent, child, style);
Console.WriteLine(frame.Items[0].Kind);
```

```text
Line
```

Unzip [the example](https://recursiveai.org/onescope/cookbook/draw-entity-links-in-replay/draw-entity-links-in-replay.zip), then `dotnet run`.

To see the line in context, install the viewer too:

```shell
dotnet tool install --global RecursiveAI.OneScope.Tool
```

After `dotnet run` writes `pair.replay`, open it from the example folder. The zip’s `Example.csproj` builds the module into `Example.dll`. The viewer draws a line to the moving child as you seek and hides the line when you turn off **Relationships**.

```shell
onescope open pair.replay \
  --module bin/Debug/net10.0/Example.dll
```

## Replay writer and drawing code

A module is your C# plugin that tells the viewer how to read and draw your replay; an overlay is a layer you can switch on and off.

The drawing snippets below are excerpts from the zip’s `PairModule.cs`, which contains `PairSource` and `PairWorld`. `PairSource` declares two overlays. Both start on:

```csharp
Overlays = [new OverlayInfo(0, "Entities", true),
            new OverlayInfo(1, "Relationships", true)]
```

`PairWorld.Draw` gives the line overlay ID `1` in `DrawStyle` and only adds it if that overlay is on:

```csharp
public void Draw(DrawList into, in DrawRequest request)
{
    var parent = Vector3.Zero;
    var child = new Vector3(3, 2 + Step, 0);
    into.Point(parent, 10, new DrawStyle(Color32.White, Entity: new EntityId(1)));
    into.Point(child, 10, new DrawStyle(Color32.White, Entity: new EntityId(2)));
    if (request.IsOn(1))
        into.Line(parent, child, new DrawStyle(Color32.White, Overlay: 1));
}
```

The replay writer below makes `pair.replay`. `PAIR` is the file signature; the last four bytes encode four simulation steps.

```csharp
// PAIR + 4 steps (little-endian).
byte[] data = [80, 65, 73, 82, 4, 0, 0, 0];
File.WriteAllBytes("pair.replay", data);
Console.WriteLine("Wrote pair.replay.");
```

```text
Wrote pair.replay.
```

The code that opens `pair.replay` throws `ScopeFormatException` (an invalid replay format error) if the file isn’t eight bytes starting with `PAIR` or its step count is outside 0–10,000.

## Related recipes

- [Write a custom replay viewer module](https://recursiveai.org/onescope/cookbook/write-a-onescope-module/)
- [Track an entity between replay sources](https://recursiveai.org/onescope/cookbook/track-entity-between-sources/)

## The short answer

In your replay module’s `IScopeWorld.Draw` method, look up both ends of the relationship in the current state and pass their positions to `DrawList.Line`. Use world coordinates, with Z set to zero for a 2D source. The viewer draws the segment from the first position to the second. `DrawList.Line` doesn’t need either entity’s ID; your module decides which entities to connect and where they are.

## How do you identify relationships between entities in a replay?

Use the relationship stored in the simulated world, not proximity on screen. Two moving objects can be close without being related. In the example, entity 2 has entity 1 as its parent in the entity tree, and the module draws a segment from 1 to 2. In a simulation with changing relationships, take the current relation and both positions from the same reconstructed step. If one endpoint is absent at that step, skip the segment rather than leaving it attached to the last position you saw.

The entity tree and the drawn segment answer different questions. The tree tells you what owns or contains an entity; the line shows where that relationship spans the scene. You can draw a line for a relation that isn’t a parent-child edge in the tree, such as a moving object’s assigned destination. Keep scene geometry in its own overlay so you can hide the links without hiding the objects. This module declares **Relationships** as overlay 1 and uses that ID in the line’s `DrawStyle`.

## Does a replay link follow an entity across sources?

A line in `DrawList` is geometry for the current frame, not a transfer between sources. For one entity that continues in a different recording, `IScopeWorld.CollectLinks` declares the two identities and their shared transfer step. The viewer then follows the selection across sources as the cursor crosses that step. It ignores the transfer if the other source isn’t open. Use a draw-list line to see a spatial relationship; use a [cross-source link](https://recursiveai.org/onescope/docs/fields-links-ghosts/#links) to follow one entity’s identity between recordings.
