Ecs system and component design

From decentraland/unity-explorer · View interactive version

Ecs system and component design is an AI agent skill from the GitHub repository decentraland/unity-explorer. ECS system and component design using Arch ECS. Use when creating or modifying systems, components, queries, cleanup, singletons, or tests -- also for code reviewing World/Entity/component usage.

How to use this skill

Skill file

ECS System & Component Design

Sources


System Design Rules

Condensed rules are in CLAUDE.md sections 1-7. This skill provides expanded patterns and guidance not covered there. For a full clean system code example, see reference.md.

Key reminders:


Querying Best Practices

Ranked from most preferred to least:

  1. Source-generated queries (preferred) -- Use [Query] attribute with [Data] parameters
  2. GetChunkIterator() -- For generic systems
  3. World.InlineQuery -- Outside systems or generic cases; uses IForEach<T> struct
  4. World.Query -- Last resort due to delegate/closure overhead

Always filter out DeleteEntityIntention. No nested queries. Use TryGet for known entities over queries.


Safe Component Mutation

// CORRECT -- ref var modifies the component in-place
ref var component = ref world.Get<MyComponent>(entity);
component.Value = 42;

// WRONG -- var copies the component; changes are lost
var component = world.Get<MyComponent>(entity);
component.Value = 42; // This modification is lost!

Component Cleanup Patterns

Cleanup must handle three triggers:

1. Component Removed

[Query] [None(typeof(PBAudioSource), typeof(DeleteEntityIntention))]
private void HandleComponentRemoval(ref AudioSourceComponent component)
    => releaseAudioSourceComponent.Update(ref component);

2. Entity Destroyed

[Query] [All(typeof(DeleteEntityIntention))]
private void HandleEntityDestruction(ref AudioSourceComponent component)
    => releaseAudioSourceComponent.Update(ref component);

3. World Disposed

public void FinalizeComponents(in Query query)
{
    World.InlineQuery<ReleaseAudioSourceComponent, AudioSourceComponent>(
        in new QueryDescription().WithAll<AudioSourceComponent>(),
        ref releaseAudioSourceComponent);
}

Cleanup tasks include: pool returns, promise invalidation/dereferencing, cache dereferencing, custom logic (e.g., avatar teardown).

Prefer ReleasePoolableComponentSystem<T, TProvider> for pooled disposals. For the full cleanup lifecycle code example with IForEach<T> struct pattern, see reference.md.

Pool hygiene in component disposal

If a component or asset wrapper rents from a pool in its constructor or factory (e.g. RENDERERS_POOL.Get(), COLLIDERS_POOL.Get()), the matching Pool.Release(...) must run in its Dispose(). Missing pool releases do not leak Unity objects (those are owned by the GameObject hierarchy and destroyed via SafeDestroy(Root)) but they strand the C# List<T> containers — the pool's working set inflates by one list per disposed instance and Get() keeps allocating fresh lists instead of reusing.

Audit pool hygiene by mirroring every Pool.Get() against a Pool.Release(...) on the disposal path. Scratch-only rentals inside using (PoolExtensions.Scope<T>.AutoScope()) blocks return automatically — those don't need explicit release.


Intention Components (Request-Response Pattern)

The codebase uses "intention" struct components to model async ECS requests. A system creates an entity with an intention; a loading system processes it and adds StreamableLoadingResult<T>.

4-step pattern:

  1. Create -- Build an AssetPromise entity with the intention component
  2. Load -- A LoadSystemBase<TAsset, TIntention> picks it up, loads the asset, adds StreamableLoadingResult<T>
  3. Consume -- The requesting system calls TryConsume / TryGetResult to retrieve the result
  4. Cleanup -- Dereference via TryDereference, destroy via ForgetLoading or Consume

Base interfaces: IAssetIntention (has CancellationTokenSource), ILoadingIntention : IAssetIntention (adds CommonLoadingArguments).

Common intention types: GetTextureIntention, GetAudioClipIntention, GetSceneFacadeIntention, DeleteEntityIntention, GetProfilesBatchIntent, and 15+ others.

See the asset-promise-lifecycle skill for full creation, polling, error handling, and cleanup patterns.


ECS Singletons

See CLAUDE.md section 8 for core singleton rules.


Testing Systems

See the testing-infrastructure skill for full test patterns, utilities, and examples.


Detailed Reference

For detailed code examples, see reference.md.


Cross-References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.