Ways to access data
How you access data in a system depends on whether you’ve used a managed SystemBase system, or an unmanaged ISystem system. The following are ways of accessing the data in a system:
- SystemState: Use the properties and methods in
SystemStateto access raw entity state data inISystemsystems.SystemBaseandSystemAPInatively use the data inSystemState. - SystemBase: Contains the same methods as
SystemState, but you can call them from aSystemBasesystem. - SystemAPI: Calls the data you can get from SystemState, but caches and updates the data for you, meaning that you can use
SystemAPImethods directly in anUpdateloop. Because you can directly use these methods inUpdate, there's no runtime cost to usingSystemAPI, so useSystemAPIto access data, wherever possible.
SystemState
You can use SystemState to get data in the following ways:
- Find out information about worlds
- Query a system to get information about it
- Get data which to add as a dependency of the system. This is similar to the way you get data with EntityManager, but with added dependencies.
World information
To find out information about worlds, you can use the following properties:
state.Worldstate.WorldUnmanagedstate.WorldUpdateAllocatorstate.GlobalSystemVersionstate.EntityManager
System information
There are several ways in which you can query the status of systems:
| API | Description |
|---|---|
state.Dependencystate.CompleteDependency |
Get or complete dependencies of the system. |
RequireForUpdateRequireAnyForUpdateShouldRunSystemEnabled |
Determine when a system needs to run. |
state.SystemHandle |
Get a system’s handle. |
state.LastSystemVersion |
Get a system’s version number. |
state.DebugName |
Get a system’s debug name. |
Dependency data
To get methods which you can add as a dependency of the system, you can use:
| API | Description |
|---|---|
GetEntityQuery |
Gets a query. Note: EntityQueryBuilder.Build is the preferred method to get queries and you should use this method wherever possible. |
GetBufferLookupGetComponentLookupGetEntityStorageInfoLookup |
Get lookups. |
GetComponentTypeHandleGetBufferTypeHandleGetEntityTypeHandleGetSharedComponentTypeHandleGetDynamicComponentTypeHandleGetDynamicSharedComponentTypeHandle |
Get type handles. |
These methods all add dependencies to the given type. For example, if you call state.GetComponentTypeHandle<MyComp>(isReadOnly: true), it adds a dependency of MyComp to be readable. This means that state.Dependency includes all earlier systems' state.Dependency for the systems which write to MyComp. GetEntityQuery has the same functionality for every component in the query, while the lookup methods add a dependency of the type.
SystemBase
All the methods in SystemState are available in SystemBase, and they're prefixed with this. rather than state.
SystemAPI
SystemAPI is a class that provides caching and utility methods for accessing data in an entity's world. It works in non-static methods in SystemBase and non-static methods in ISystem that take ref SystemState as a parameter. Because you can directly use these methods in Update, there's no runtime cost to using SystemAPI, so use SystemAPI to access data, wherever possible.
For more information, refer to the SystemAPI overview documentation.