Game.Simulation.WildlifeAISystem
Assembly:
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
WildlifeAISystem is a simulation system that updates wildlife and grouped creatures each frame (on a scheduled interval). It runs two Burst-compiled IJobChunk jobs:
- WildlifeTickJob — updates individual wildlife entities (movement, idling/wandering state, path end handling, navigation target selection including interaction with terrain and water).
- WildlifeGroupTickJob — updates group members (syncs flags with leader, handles stuck group members).
The system also integrates with TerrainSystem and WaterSystem to sample heights/surfaces, uses an EndFrameBarrier to record EntityCommandBuffer commands for deletion/component additions, and sets up EntityQueries for regular and grouped wildlife.
Fields
-
private EndFrameBarrier m_EndFrameBarrier
m_EndFrameBarrier is used to create an EntityCommandBuffer (parallel writer) so jobs can issue structural changes (e.g., marking entities Deleted or adding components) safely from jobs. -
private TerrainSystem m_TerrainSystem
Reference to the TerrainSystem used to obtain terrain height data for navigation/target placement. -
private WaterSystem m_WaterSystem
Reference to the WaterSystem used to obtain water surface data for swim/fly depth sampling. -
private EntityQuery m_CreatureQuery
EntityQuery selecting non-grouped wildlife (components: Wildlife, Animal, PrefabRef, UpdateFrame; excludes GroupMember, Deleted, Temp, Stumbling). Used to schedule WildlifeTickJob. -
private EntityQuery m_GroupCreatureQuery
EntityQuery selecting group members (includes GroupMember). Used to schedule WildlifeGroupTickJob. -
private TypeHandle __TypeHandle
A generated helper struct holding EntityTypeHandle, ComponentTypeHandles and ComponentLookups required to run the jobs. It assigns handles from a SystemState in OnCreateForCompiler.
Properties
This system exposes no public properties.
Constructors
public WildlifeAISystem()
Default constructor. Marked with [Preserve] attribute in source; standard boilerplate for ECS systems.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the system update interval in frames. In source it returns 16, making the system run every 16 frames (subject to offset). -
public override int GetUpdateOffset(SystemUpdatePhase phase)
Returns the system update offset. In source it returns 13 (so combined with interval schedules when the system executes). -
protected override void OnCreate()
Initializes the system: obtains EndFrameBarrier, TerrainSystem, WaterSystem and creates EntityQueries for both individual wildlife and group members. Also registers the queries as required for updates (RequireAnyForUpdate). This sets up runtime dependencies and ensures system only runs if relevant entities exist. -
protected override void OnUpdate()
Creates and schedules the two Burst IJobChunk jobs: - WildlifeTickJob (scheduled parallel for m_CreatureQuery). This job:
- Reads PrefabRef, Animal, Wildlife, AnimalNavigation, and other data.
- Uses a per-job Random seed.
- Checks if entity is stuck and deletes it.
- Controls roaming flag based on whether the entity has a lane.
- When path end is reached selects new navigation target or deletes entity if owner/transform missing.
- Uses Terrain and Water data to place target positions correctly (swim depth, fly height, terrain sampling).
- WildlifeGroupTickJob (scheduled parallel for m_GroupCreatureQuery). This job:
- Ensures AnimalCurrentLane component exists.
- Syncs certain flags with the group leader (swimming/flying target, roaming).
- Deletes group members that are stuck.
OnUpdate also: - Obtains terrain height data synchronously from TerrainSystem. - Obtains water surface data from WaterSystem (also returns a JobHandle dependency). - Adds required CPU readers to systems so data access is synchronized. - Adds the final job handle to the EndFrameBarrier so the command buffer is valid until frame end.
-
protected override void OnCreateForCompiler()
Internal helper invoked by generated code to assign query & type handles needed by the jobs. Calls __AssignQueries and __TypeHandle.__AssignHandles. -
private void __AssignQueries(ref SystemState state)
Generated helper used by OnCreateForCompiler; currently contains no active queries in the decompiled source (creates and immediately disposes an EntityQueryBuilder). Kept for compatibility with generated code.
Nested types (brief)
-
WildlifeTickJob : IJobChunk
(BurstCompile)
Job that updates individual wildlife entities. Handles idling/wandering state, roaming toggling, selecting new navigation targets at path end, sampling terrain/water heights, and deleting invalid/unowned entities. -
WildlifeGroupTickJob : IJobChunk
(BurstCompile)
Job that updates grouped creatures (GroupMember). Ensures AnimalCurrentLane exists, checks for stuck condition, synchronizes roaming/swimming/flying flags with the group leader, and deletes stuck group members. -
TypeHandle
(struct)
Generated struct holding EntityTypeHandle, ComponentTypeHandles and ComponentLookups used by the jobs. Contains __AssignHandles to populate handles from a SystemState.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Example: obtain systems and set up queries as the original system does
m_EndFrameBarrier = base.World.GetOrCreateSystemManaged<EndFrameBarrier>();
m_TerrainSystem = base.World.GetOrCreateSystemManaged<TerrainSystem>();
m_WaterSystem = base.World.GetOrCreateSystemManaged<WaterSystem>();
m_CreatureQuery = GetEntityQuery(
ComponentType.ReadWrite<Game.Creatures.Wildlife>(),
ComponentType.ReadWrite<Animal>(),
ComponentType.ReadOnly<PrefabRef>(),
ComponentType.ReadOnly<UpdateFrame>(),
ComponentType.Exclude<GroupMember>(),
ComponentType.Exclude<Deleted>(),
ComponentType.Exclude<Temp>(),
ComponentType.Exclude<Stumbling>());
m_GroupCreatureQuery = GetEntityQuery(
ComponentType.ReadWrite<Game.Creatures.Wildlife>(),
ComponentType.ReadWrite<Animal>(),
ComponentType.ReadOnly<GroupMember>(),
ComponentType.ReadOnly<PrefabRef>(),
ComponentType.ReadOnly<UpdateFrame>(),
ComponentType.Exclude<Deleted>(),
ComponentType.Exclude<Temp>(),
ComponentType.Exclude<Stumbling>());
RequireAnyForUpdate(m_CreatureQuery, m_GroupCreatureQuery);
}
Notes: - The system relies on Unity.Entities low-level job APIs (IJobChunk, ComponentTypeHandle, ComponentLookup) and Burst for performance. - Structural changes (adding Deleted, adding AnimalCurrentLane) are issued via an EndFrameBarrier's EntityCommandBuffer.ParallelWriter from jobs. - The system runs less frequently than every frame (interval 16 with offset 13), so wildlife updates are batched for performance.