Skip to content

Change Tracking ​

Opt in per pool. Get stays untracked; write through Modify instead:

csharp
class SyncTransformSystem : IInitSystem, IRunSystem {
    Filter _views;
    ComponentPool<Position> _positions;
    int _lastSeen;

    public void Init(World world, SharedData shared) {
        _views = world.Filter().Inc<Position>().Inc<View>().End();
        _positions = world.Pool<Position>();
        _positions.TrackChanges();
    }

    public void Run(World world) {
        foreach (int e in _views) {
            if (!_positions.ChangedSince(e, _lastSeen)) {
                continue;
            }
            SyncTransform(e);
        }
        _lastSeen = world.ChangeVersion;     // bookmark: "everything up to here is handled"
    }

    void SyncTransform(int e) {
        ref var pos = ref _positions.Get(e);
        Console.WriteLine($"entity {e} moved to ({pos.X}, {pos.Y})");
    }
}

Elsewhere, writers mark what they touched:

csharp
_positions.Modify(e).X += 1;             // marks changed
_positions.MarkChanged(e);               // without reading

Versions come from a world-wide counter that Add, Modify and MarkChanged advance, so a consumer sees exactly the changes made since its own last run, whatever the system order.

API ​

MemberMeaning
pool.TrackChanges()Start recording change versions. Components already in the pool count as changed now. Calling it again does nothing.
pool.TracksChangesWhether the pool records versions.
pool.Modify(e)ref to the component, like Get, and marks it changed.
pool.MarkChanged(e)Marks the component changed without reading it.
pool.ChangedVersion(e)Version recorded by the last Add, Modify or MarkChanged.
pool.ChangedSince(e, version)true when the component was added or modified after version (a value of world.ChangeVersion).
world.ChangeVersionCurrent value of the world-wide counter; store it as a bookmark at the end of Run.

WARNING

ChangedVersion and ChangedSince throw on a pool that does not track changes — call TrackChanges() once (e.g. in Init). Modify and MarkChanged work on any pool; without tracking they record nothing.

Counter wrap ​

The counter passes int.MaxValue after two billion counted writes (hours of play for a game that marks a few thousand components per frame) and starts again at 1. ChangedSince keeps working across that, and nothing changes before the first wrap. After it, a bookmark is exact for the next 2^30 (about a billion) counted writes; against an older one, a component written before it can be reported as changed. Compare versions only with ChangedSince: a hand-written pool.ChangedVersion(e) > bookmark works until the wrap and breaks after it.

Changed in 2.5.0

Up to 2.4.0 the counter went negative at the wrap: ChangedSince then reported nothing as changed against a bookmark taken before it, and old components as changed against one taken after it.

Tracking costs one int per component and one store per Add, Remove and Modify. Writes through Get or RawData are invisible to it; CommandBuffer.Set on an existing component counts as a change.