toentityarray-snapshot-pattern

Snapshot entity IDs and component data into NativeArrays for safe structural changes.

6|Updated Apr 2, 2026
One-click install
npx skills add https://github.com/dyCuong03/unity-agent-team --skill toentityarray-snapshot-pattern
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: toentityarray-snapshot-pattern
Source: https://github.com/dyCuong03/unity-agent-team/tree/main/.claude/skills/unity-dots/toentityarray-snapshot-pattern
Command: npx skills add https://github.com/dyCuong03/unity-agent-team --skill toentityarray-snapshot-pattern

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Prevents iterator invalidation and safety exceptions when iterating entity data while performing structural changes that may alter archetypes or entity state.

Core Features & Use Cases

  • Safe iterate + structurally change workflow: Snapshot entity IDs and per-entity component data into NativeArrays before making structural changes.
  • Per-entity conditional initialization: Use snapshot arrays to compute and write component data safely after structural changes.
  • Main-thread ECS correctness: Applies to EntityQuery/SystemAPI patterns where batch structural operations are insufficient because each entity’s condition differs.

Quick Start

Ask the AI to “Generate a Unity DOTS system that snapshots matching entities with ToEntityArray and ToComponentDataArray, performs AddComponent/RemoveComponent on the query, then initializes per-entity data in a for-loop using the snapshot arrays.”

Frequently Asked Questions about toentityarray-snapshot-pattern

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why does my Unity DOTS EntityQuery iterator throw a safety exception when adding components?

Iterator invalidation occurs because structural changes alter archetypes while you are still iterating. You must snapshot entity IDs and component data into NativeArrays before performing structural changes to avoid this exception.

How do I safely iterate entities and perform structural changes in Unity ECS?

To safely iterate and perform structural changes, build an EntityQuery, copy matching entities and component data into NativeArrays using an appropriate allocator, execute structural changes on the query, then iterate the snapshot arrays afterward.

When should I use ToEntityArray and ToComponentDataArray instead of batch structural changes in Unity DOTS?

Use ToEntityArray and ToComponentDataArray when per-entity conditional logic differs and query-level batch overloads cannot express entity-specific changes. Snapshotting allows safe computation and initialization for each individual entity.

Can I modify entity component data directly while iterating an EntityQuery on the main thread?

No, modifying archetype state during an active query iteration causes safety exceptions. You must first snapshot the required entity IDs and component data into NativeArrays, stop iterating, perform the structural changes, then iterate the arrays.

What allocator should I use for snapshotting EntityQuery data during structural changes?

You must use an appropriate allocator to copy ToEntityArray and ToComponentDataArray data into NativeArrays. This snapshot persists safely outside the query iteration scope, allowing structural changes and subsequent per-entity data initialization.