### Systems in ECS
A typical system operates on a set of entities that have specific components. The system identifies
the components of interest, reading and writing data, and performing other entity operations as appropriate.
The following example shows a basic system that schedules an [IJobEntity] job to iterate over entities.
In this example, the system iterates over all entities with both a Position and a Velocity component and
updates Position based on the delta time elapsed since the last frame.
#### System lifecycle callbacks
You can define a set of system lifecycle event functions when you implement a system. The runtime invokes these
functions in the following order:
" ComponentSystemBase.OnCreate -- called when the system is created.
" ComponentSystemBase.OnStartRunning -- before the first OnUpdate and whenever the system resumes
running.
" SystemBase.OnUpdate -- every frame as long as the system has work to do (see
ComponentSystemBase.ShouldRunSystem) and the system is ComponentSystemBase.Enabled.
" ComponentSystemBase.OnStopRunning -- whenever the system stops updating because it finds no
entities matching its queries. Also called before OnDestroy.
" ComponentSystemBase.OnDestroy -- when the system is destroyed.
All of these functions are executed on the main thread. To perform work on background threads, you can schedule
jobs from the SystemBase.OnUpdate function.
#### System update order
The runtime executes systems in the order determined by their ComponentSystemGroup. Place a system
in a group using UpdateInGroupAttribute. Use UpdateBeforeAttribute and
UpdateAfterAttribute to specify the execution order within a group.
If you do not explicitly place a system in a specific group, the runtime places it in the default WorldSimulationSystemGroup. By default, all systems are discovered, instantiated, and added to the
default World. You can use the DisableAutoCreationAttribute to prevent a system from being
created automatically.
#### Entity queries
A system caches all queries created through iteration constructs (such as an [IJobEntity] schedule or a
SystemAPI.Query loop), through ComponentSystemBase.GetEntityQuery, or through
ComponentSystemBase.RequireForUpdate. By default, a system calls OnUpdate() every frame. You can use
the RequireMatchingQueriesForUpdateAttribute to make the system only update when one of these cached
queries finds entities. See ComponentSystemBase.ShouldRunSystem for more details on whether
a system will update.
#### Entity iteration options
" [IJobEntity] — define a struct with an Execute method taking ref / in component parameters. Run it using
Schedule(), ScheduleParallel(), or Run(). Jobs execute asynchronously, and cannot safely access data
outside of their own instance. Put any required data into fields and set
them when you create the job (for example, new MyJob { DeltaTime = SystemAPI.Time.DeltaTime }).
" __SystemAPI.Query — iterate immediately on the main thread (optionally Burst compiled) using
@@foreach@@ over tuples of @@RefRO<T>@@ / @@RefRW<T>@@ wrappers.
" IJobChunk — implement job structs that run per chunk.
" C# Jobs (IJob, IJobParallelFor, etc.)__ — general-purpose, non-ECS-specific jobs.
Use such jobs for computation or processing that does not require an entity query.
#### System attributes
You can use a number of attributes on your SystemBase implementation to control when it updates:
" UpdateInGroupAttribute -- place the system in a ComponentSystemGroup.
" UpdateBeforeAttribute -- always update the system before another system in the same group.
" UpdateAfterAttribute -- always update the system after another system in the same group.
" RequireMatchingQueriesForUpdateAttribute -- skip OnUpdate if every EntityQuery used by
the system is empty.
" DisableAutoCreationAttribute -- do not create the system automatically.
" AlwaysSynchronizeSystemAttribute -- force a sync point before invoking
OnUpdate.
Migration note: Entities.ForEach and Job.WithCode are deprecated as of Entities 1.4. Use [IJobEntity], [IJobChunk] or
SystemAPI.Query. For more information, refer to the upgrade guide.
[JobHandle]: xref:Unity.Jobs.JobHandle
[JobHandle.CompleteDependencies]: xref:Unity.Jobs.JobHandle.CombineDependencies
[C# Job]: xref:JobSystem
[ECB]: xref:Unity.Entities.EntityCommandBuffer
[ComponentSystemBase.GetEntityQuery]: xref:Unity.Entities.ComponentSystemBase.GetEntityQuery"
[ComponentSystemBase.RequireForUpdate]: xref:Unity.Entities.ComponentSystemBase.RequireForUpdate*