# SystemBase

> Implement SystemBase to create a system in ECS.

## Definition

* **Type:** Class
* **Namespace:** [Unity.Entities](/engine/7000.0/script-reference/unity/entities.md)
* **Assembly:** UnityEngine.EntitiesModule
* **Inherits from:** [ComponentSystemBase](/engine/7000.0/script-reference/unity/entities/componentsystembase.md)

```csharp
public abstract class SystemBase : ComponentSystemBase
```

## Remarks

### 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()](/engine/7000.0/script-reference/unity/entities/componentsystembase/oncreate.md) -- called when the system is created. " [ComponentSystemBase.OnStartRunning()](/engine/7000.0/script-reference/unity/entities/componentsystembase/onstartrunning.md) -- before the first OnUpdate and whenever the system resumes running. " [SystemBase.OnUpdate](/engine/7000.0/script-reference/unity/entities/systembase/onupdate.md) -- every frame as long as the system has work to do (see [ComponentSystemBase.ShouldRunSystem()](/engine/7000.0/script-reference/unity/entities/componentsystembase/shouldrunsystem.md)) and the system is [ComponentSystemBase.Enabled](/engine/7000.0/script-reference/unity/entities/componentsystembase/enabled.md). " [ComponentSystemBase.OnStopRunning()](/engine/7000.0/script-reference/unity/entities/componentsystembase/onstoprunning.md) -- whenever the system stops updating because it finds no entities matching its queries. Also called before OnDestroy. " [ComponentSystemBase.OnDestroy()](/engine/7000.0/script-reference/unity/entities/componentsystembase/ondestroy.md) -- 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](/engine/7000.0/script-reference/unity/entities/systembase/onupdate.md) function.

#### System update order

The runtime executes systems in the order determined by their [ComponentSystemGroup](/engine/7000.0/script-reference/unity/entities/componentsystemgroup.md). Place a system in a group using [UpdateInGroupAttribute](/engine/7000.0/script-reference/unity/entities/updateingroupattribute.md). Use [UpdateBeforeAttribute](/engine/7000.0/script-reference/unity/entities/updatebeforeattribute.md) and [UpdateAfterAttribute](/engine/7000.0/script-reference/unity/entities/updateafterattribute.md) 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 [World](/engine/7000.0/script-reference/unity/entities/world.md)[SimulationSystemGroup](/engine/7000.0/script-reference/unity/entities/simulationsystemgroup.md). By default, all systems are discovered, instantiated, and added to the default World. You can use the [DisableAutoCreationAttribute](/engine/7000.0/script-reference/unity/entities/disableautocreationattribute.md) 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](/engine/7000.0/script-reference/unity/entities/systemapi/query.md) loop), through [ComponentSystemBase.GetEntityQuery](/engine/7000.0/script-reference/unity/entities/componentsystembase/getentityquery.md), or through [ComponentSystemBase.RequireForUpdate](/engine/7000.0/script-reference/unity/entities/componentsystembase/requireforupdate.md). By default, a system calls `OnUpdate()` every frame. You can use the [RequireMatchingQueriesForUpdateAttribute](/engine/7000.0/script-reference/unity/entities/requirematchingqueriesforupdateattribute.md) to make the system only update when one of these cached queries finds entities. See [ComponentSystemBase.ShouldRunSystem()](/engine/7000.0/script-reference/unity/entities/componentsystembase/shouldrunsystem.md) 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](/engine/7000.0/script-reference/unity/entities/systemapi/query.md)** — iterate immediately on the main thread (optionally Burst compiled) using `foreach` over tuples of `RefRO<T>` / `RefRW<T>` wrappers. " **[IJobChunk](/engine/7000.0/script-reference/unity/entities/ijobchunk.md)** — implement job structs that run per chunk. " **C# Jobs ([IJob](/engine/7000.0/script-reference/unity/jobs/ijob.md), [IJobParallelFor](/engine/7000.0/script-reference/unity/jobs/ijobparallelfor.md), 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](/engine/7000.0/script-reference/unity/entities/updateingroupattribute.md) -- place the system in a [ComponentSystemGroup](/engine/7000.0/script-reference/unity/entities/componentsystemgroup.md). " [UpdateBeforeAttribute](/engine/7000.0/script-reference/unity/entities/updatebeforeattribute.md) -- always update the system before another system in the same group. " [UpdateAfterAttribute](/engine/7000.0/script-reference/unity/entities/updateafterattribute.md) -- always update the system after another system in the same group. " [RequireMatchingQueriesForUpdateAttribute](/engine/7000.0/script-reference/unity/entities/requirematchingqueriesforupdateattribute.md) -- skip `OnUpdate` if every EntityQuery used by the system is empty. " [DisableAutoCreationAttribute](/engine/7000.0/script-reference/unity/entities/disableautocreationattribute.md) -- do not create the system automatically. " [AlwaysSynchronizeSystemAttribute](/engine/7000.0/script-reference/unity/entities/alwayssynchronizesystemattribute.md) -- 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](/engine/7000.0/script-reference/unity/entities/systemapi/query.md). 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*

## Properties

| Property                                                                                        | Description                                                                                           |
| ----------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| [CheckedStateRef](/engine/7000.0/script-reference/unity/entities/systembase/checkedstateref.md) | The [SystemState](/engine/7000.0/script-reference/unity/entities/systemstate.md) for this SystemBase. |
| [Dependency](/engine/7000.0/script-reference/unity/entities/systembase/dependency.md)           | The ECS-related data dependencies of the system.                                                      |

## Methods

| Method                                                                                                                | Description                                                                                                                                                                   |
| --------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [CompleteDependency](/engine/7000.0/script-reference/unity/entities/systembase/completedependency.md)                 | Completes job handles registered with this system. See [SystemBase.Dependency](/engine/7000.0/script-reference/unity/entities/systembase/dependency.md) for more information. |
| [GetBufferLookup](/engine/7000.0/script-reference/unity/entities/systembase/getbufferlookup.md)                       | Manually gets a BufferLookup\\\<T\\> object that can access a [DynamicBuffer\<T>](/engine/7000.0/script-reference/unity/entities/dynamicbuffer1.md).                          |
| [GetComponentLookup](/engine/7000.0/script-reference/unity/entities/systembase/getcomponentlookup.md)                 | Manually gets a dictionary-like container containing all components of type T, keyed by Entity.                                                                               |
| [GetEntityStorageInfoLookup](/engine/7000.0/script-reference/unity/entities/systembase/getentitystorageinfolookup.md) | Manually gets an EntityStorageInfoLookup object that can access a [EntityStorageInfo](/engine/7000.0/script-reference/unity/entities/entitystorageinfo.md).                   |
| [HasBuffer](/engine/7000.0/script-reference/unity/entities/systembase/hasbuffer.md)                                   | Checks whether an entity has a dynamic buffer of a specific IBufferElementData type.                                                                                          |
| [OnUpdate](/engine/7000.0/script-reference/unity/entities/systembase/onupdate.md)                                     | Implement `OnUpdate()` to perform the major work of this system.                                                                                                              |
| [Update](/engine/7000.0/script-reference/unity/entities/systembase/update.md)                                         | Update the system manually.                                                                                                                                                   |

## Inheritance

**Inherited Members:**

* [ComponentSystemBase.OnCreate()](/engine/7000.0/script-reference/unity/entities/componentsystembase/oncreate.md)
* [ComponentSystemBase.OnStartRunning()](/engine/7000.0/script-reference/unity/entities/componentsystembase/onstartrunning.md)
* [ComponentSystemBase.OnStopRunning()](/engine/7000.0/script-reference/unity/entities/componentsystembase/onstoprunning.md)
* [ComponentSystemBase.OnDestroy()](/engine/7000.0/script-reference/unity/entities/componentsystembase/ondestroy.md)
* [ComponentSystemBase.ShouldRunSystem()](/engine/7000.0/script-reference/unity/entities/componentsystembase/shouldrunsystem.md)
* [ComponentSystemBase.GetComponentTypeHandle\<T>(bool)](/engine/7000.0/script-reference/unity/entities/componentsystembase/getcomponenttypehandle.md)
* [ComponentSystemBase.GetDynamicComponentTypeHandle(ComponentType)](/engine/7000.0/script-reference/unity/entities/componentsystembase/getdynamiccomponenttypehandle.md)
* [ComponentSystemBase.GetBufferTypeHandle\<T>(bool)](/engine/7000.0/script-reference/unity/entities/componentsystembase/getbuffertypehandle.md)
* [ComponentSystemBase.GetSharedComponentTypeHandle\<T>()](/engine/7000.0/script-reference/unity/entities/componentsystembase/getsharedcomponenttypehandle.md)
* [ComponentSystemBase.GetDynamicSharedComponentTypeHandle(ComponentType)](/engine/7000.0/script-reference/unity/entities/componentsystembase/getdynamicsharedcomponenttypehandle.md)
* [ComponentSystemBase.GetEntityTypeHandle()](/engine/7000.0/script-reference/unity/entities/componentsystembase/getentitytypehandle.md)
* [ComponentSystemBase.RequireForUpdate(EntityQuery)](/engine/7000.0/script-reference/unity/entities/componentsystembase/requireforupdate.md)
* [ComponentSystemBase.RequireAnyForUpdate(EntityQuery\[\])](/engine/7000.0/script-reference/unity/entities/componentsystembase/requireanyforupdate.md#requireanyforupdate\(entityquery\))
* [ComponentSystemBase.RequireAnyForUpdate(NativeArray\<EntityQuery>)](/engine/7000.0/script-reference/unity/entities/componentsystembase/requireanyforupdate.md#requireanyforupdate\(nativearrayentityquery\))
* [ComponentSystemBase.RequireForUpdate\<T>()](/engine/7000.0/script-reference/unity/entities/componentsystembase/requireforupdate.md)
* [ComponentSystemBase.TryGetSingleton\<T>(T)](/engine/7000.0/script-reference/unity/entities/componentsystembase/trygetsingleton.md)
* [ComponentSystemBase.TryGetSingletonBuffer\<T>(DynamicBuffer\<T>)](/engine/7000.0/script-reference/unity/entities/componentsystembase/trygetsingletonbuffer.md)
* [ComponentSystemBase.SetSingleton\<T>(T)](/engine/7000.0/script-reference/unity/entities/componentsystembase/setsingleton.md)
* [ComponentSystemBase.GetEntityQuery(ComponentType\[\])](/engine/7000.0/script-reference/unity/entities/componentsystembase/getentityquery.md#getentityquery\(componenttype\))
* [ComponentSystemBase.GetEntityQuery(NativeArray\<ComponentType>)](/engine/7000.0/script-reference/unity/entities/componentsystembase/getentityquery.md#getentityquery\(nativearraycomponenttype\))
* [ComponentSystemBase.GetEntityQuery(EntityQueryDesc\[\])](/engine/7000.0/script-reference/unity/entities/componentsystembase/getentityquery.md#getentityquery\(entityquerydesc\))
* [ComponentSystemBase.GetEntityQuery(EntityQueryBuilder)](/engine/7000.0/script-reference/unity/entities/componentsystembase/getentityquery.md#getentityquery\(entityquerybuilder\))
* [ComponentSystemBase.Enabled()](/engine/7000.0/script-reference/unity/entities/componentsystembase/enabled.md)
* [ComponentSystemBase.EntityQueries()](/engine/7000.0/script-reference/unity/entities/componentsystembase/entityqueries.md)
* [ComponentSystemBase.GlobalSystemVersion()](/engine/7000.0/script-reference/unity/entities/componentsystembase/globalsystemversion.md)
* [ComponentSystemBase.LastSystemVersion()](/engine/7000.0/script-reference/unity/entities/componentsystembase/lastsystemversion.md)
* [ComponentSystemBase.EntityManager()](/engine/7000.0/script-reference/unity/entities/componentsystembase/entitymanager.md)
* [ComponentSystemBase.World()](/engine/7000.0/script-reference/unity/entities/componentsystembase/world.md)
* [ComponentSystemBase.SystemHandle()](/engine/7000.0/script-reference/unity/entities/componentsystembase/systemhandle.md)
* [ComponentSystemBase.WorldUpdateAllocator()](/engine/7000.0/script-reference/unity/entities/componentsystembase/worldupdateallocator.md)
