Game.Objects.SubObjectHiddenSystem
Assembly:
Namespace: Game.Objects
Type: class
Base: GameSystemBase
Summary:
SubObjectHiddenSystem is an ECS system that keeps sub-objects' Hidden state in sync with their owners and temporary replacement objects. It runs two Burst‑compiled chunk jobs:
- FillTempMapJob: scans entities with Temp to build a map of "original → temporary" entities to prevent temporarily replaced objects from being re‑shown.
- HiddenSubObjectJob: iterates entities that have SubObject buffers (and various filters), and for each parent entity ensures that sub-objects are either marked Hidden or made Visible (removing Hidden) depending on owner relationships, Temp entries, and whether a sub-object is a building. Changes are applied via a ModificationBarrier5 command buffer so they are safely produced from jobs.
The system uses parallel jobs, ComponentLookup/BufferLookup/TypeHandles, and a NativeParallelHashMap as a temporary data structure. It excludes Deleted entities and avoids changing building sub-objects.
Fields
-
private ModificationBarrier5 m_ModificationBarrier
Creates command buffers for recording component add/remove operations produced by jobs. The system obtains this barrier in OnCreate and uses it to write component changes from parallel jobs. -
private EntityQuery m_HiddenQuery
Query used to find entities containing Hidden + SubObject (and an alternate query for Hidden + Object + Owner excluding Vehicle/Creature/Building/Deleted). Required for updating the hidden state of sub-objects each frame. -
private EntityQuery m_TempQuery
Query used to find entities containing Temp + Object (excluding Deleted). Used to populate the temporary mapping of originals → temporaries to avoid unhiding originals while a temp is present. -
private TypeHandle __TypeHandle
Holds cached Entity/Component/Buffer type handles and ComponentLookup/BufferLookup references used by the jobs. Assigned in OnCreateForCompiler to avoid repeated Get*Handle calls inside jobs.
Properties
- (none)
Constructors
public SubObjectHiddenSystem()
Default constructor (marked [Preserve]). Standard system initialization is performed in OnCreate rather than the ctor.
Methods
-
[Preserve] protected override void OnCreate()
: System.Void
Initializes the ModificationBarrier5, constructs the m_HiddenQuery and m_TempQuery with the appropriate component filters, and calls RequireForUpdate(m_HiddenQuery) so the system runs only when relevant entities exist. Also prepares internal state for updates. -
[Preserve] protected override void OnUpdate()
: System.Void
Main update method: - Allocates a NativeParallelHashMap tempMap (sized from the m_TempQuery count).
- Schedules FillTempMapJob (parallel) to populate tempMap with temporary-object relationships (original → temp entity).
- Schedules HiddenSubObjectJob (parallel) which:
- Walks through entities with SubObject buffers and decides whether sub-objects should be Hidden or Visible.
- Uses cached ComponentLookup/BufferLookup and the tempMap to make decisions.
- Recursively processes nested sub-objects via BufferLookup recursion.
- Adds/Removes Hidden and BatchesUpdated components using the command buffer (m_ModificationBarrier).
- Disposes the tempMap with the jobHandle dependency and registers the producer job handle with the modification barrier.
-
Sets base.Dependency to the returned jobHandle.
-
protected override void OnCreateForCompiler()
: System.Void
Used in generated/compiled code paths to assign queries and call __TypeHandle.__AssignHandles to initialize type handles for runtime use. -
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
: System.Void
Internal helper (compiler-generated placeholder) for query assignment; invoked from OnCreateForCompiler. -
Private nested job types (important behavior described):
FillTempMapJob : IJobChunk
- Reads Entity, Owner, Temp components and uses ComponentLookup
to decide which originals are replaced by temps. Adds mapping original → temp to NativeParallelHashMap via AsParallelWriter for later checks. - Ensures owner relationships without Temp keep correct mapping entries.
- Reads Entity, Owner, Temp components and uses ComponentLookup
HiddenSubObjectJob : IJobChunk
- Reads Entity, SubObject buffers, Object, Owner, Vehicle, Creature, Building component types, and uses ComponentLookup
, ComponentLookup , ComponentLookup , and BufferLookup . - For entities that are vehicles/creatures/buildings or have no Owner, it ensures sub-objects are Hidden.
- For entities with owners: if owner is not Hidden and neither the entity nor the owner appears in tempMap, it makes sub-objects Visible (removes Hidden and adds BatchesUpdated). Otherwise ensures sub-objects are Hidden.
- Uses recursive EnsureHidden/EnsureVisible helpers to traverse nested sub-objects.
- Writes component add/remove operations to an EntityCommandBuffer.ParallelWriter obtained from m_ModificationBarrier.
- Uses stackalloc-backed StackList to collect entities to change in a batch for the command buffer calls.
- Reads Entity, SubObject buffers, Object, Owner, Vehicle, Creature, Building component types, and uses ComponentLookup
Notes on behavior: - Building sub-objects are not toggled by this system (Building component prevents change). - The Hidden state is applied recursively to nested sub-objects when owner matches. - Temp objects are used to prevent original objects from being made visible while a temporary replacement exists.
Usage Example
This system runs automatically as part of the ECS world. Example showing concept of how Hidden and Temp affect sub-objects (pseudo/modder example):
// Mark an entity (e.g. a subobject) as temporarily replacing another:
entityManager.AddComponentData(tempEntity, new Temp { m_Original = originalEntity });
// Mark an owner as hidden so its subobjects are forced hidden:
entityManager.AddComponent<Hidden>(ownerEntity);
// After the system runs, sub-objects whose owner is Hidden (and not buildings)
// will have the Hidden component added. If the owner loses Hidden and there is
// no Temp mapping, Hidden will be removed and BatchesUpdated added so rendering
// batches can update.
Additional notes for modders: - The system is Burst-compiled and schedules parallel chunk jobs — avoid making unsafe side-effects from outside ECS or relying on immediate component changes. - Changes are applied via a ModificationBarrier5 command buffer; to see results immediately in pure managed code you must respect job dependencies or run after the system update completes. - If you add/remove Hidden or Temp components yourself, ensure your operations do not conflict (race) with this system's jobs — use proper dependency handling when scheduling your own jobs.