Skip to content

Game.Prefabs.Modes.CrimeAccumulationGlobalMode

Assembly: Assembly-CSharp
Namespace: Game.Prefabs.Modes

Type: class

Base: EntityQueryModePrefab

Summary:
CrimeAccumulationGlobalMode is a prefab-mode component used to apply a global multiplier to all CrimeAccumulationData components that match the mode's entity query. It uses a Burst-compiled IJobChunk (ModeJob) and ScheduleParallel to efficiently modify the m_CrimeRate field on matching entities in bulk. The mode also provides logic to restore per-prefab default values using the PrefabSystem when the mode is removed or reset.


Fields

  • public float m_CrimeRateMultiplier
    This public field holds the multiplier applied to CrimeAccumulationData.m_CrimeRate for every matching entity when the mode is applied. Typical values >0 (e.g., 1.0 = no change, 0.5 = halve crime rate, 2.0 = double). Can be set in the prefab inspector.

  • private struct ModeJob (BurstCompiled)
    Nested Burst-compiled IJobChunk implementation that performs the per-chunk modification:

  • public float m_CrimeRateMultiplier — local copy of the multiplier used inside the job.
  • public ComponentTypeHandle<CrimeAccumulationData> m_CrimeAccumulationType — ComponentTypeHandle used to access the CrimeAccumulationData array for the chunk (isReadOnly: false).
  • Execute(in ArchetypeChunk chunk, ...) — iterates the chunk's CrimeAccumulationData array, multiplies each element's m_CrimeRate by m_CrimeRateMultiplier and writes it back. This job is scheduled via ScheduleParallel for multithreaded execution.

Properties

  • This class does not declare public C# properties. It exposes the public field m_CrimeRateMultiplier and overrides methods from the base class to control behavior.

Constructors

  • public CrimeAccumulationGlobalMode()
    No explicit constructor is declared in the source — the default parameterless constructor from C# is used. Initialization of behavior is driven by the public field values (m_CrimeRateMultiplier) and the prefab system/editor.

Methods

  • public override EntityQueryDesc GetEntityQueryDesc()
    Returns an EntityQueryDesc that targets entities containing CrimeAccumulationData. Implementation:
  • Builds an EntityQueryDesc with All = ComponentType.ReadOnly() so the mode operates on all entities that have CrimeAccumulationData.

  • protected override void RecordChanges(EntityManager entityManager, Entity entity)
    Records current data for a single entity so it can be restored later. Implementation:

  • Calls entityManager.GetComponentData(entity) to ensure the current component value is recorded by the prefab-mode system (base class behavior expects this).

  • public override JobHandle ApplyModeData(EntityManager entityManager, EntityQuery requestedQuery, JobHandle deps)
    Schedules the ModeJob to apply the multiplier to all matching entities. Implementation details:

  • Constructs a ModeJob with m_CrimeRateMultiplier set from the prefab field and a writable ComponentTypeHandle for CrimeAccumulationData obtained from entityManager.
  • Uses JobChunkExtensions.ScheduleParallel(job, requestedQuery, deps) to schedule the job for parallel execution and returns the resulting JobHandle.
  • Note: Because the job writes to CrimeAccumulationData, the ComponentTypeHandle is requested as not read-only.

  • public override void RestoreDefaultData(EntityManager entityManager, ref NativeArray<Entity> entities, PrefabSystem prefabSystem)
    Restores the default crime rate values from prefab data for the provided entities. Implementation details:

  • Iterates the provided entity array.
  • For each entity, tries to fetch the original prefab (PrefabBase) using prefabSystem.TryGetPrefab. If found, attempts to get the CrimeAccumulation component (from prefab data).
  • If prefab/component found: reads the prefab's CrimeAccumulation.m_CrimeRate and writes it into the entity's CrimeAccumulationData component using entityManager.SetComponentData.
  • If prefab data is missing, logs a warning via ComponentBase.baseLog.Warn and skips that entity.
  • This method ensures that removing the mode or resetting modes will restore per-prefab default crime rates rather than leaving the multiplied values.

Implementation notes / concurrency considerations: - The ModeJob uses Burst and ScheduleParallel to modify components efficiently. Ensure dependency chains are respected when calling ApplyModeData (use returned JobHandle). - The ComponentTypeHandle used for writes must be obtained each time from the EntityManager (as done in ApplyModeData) so that structural changes/versions are handled correctly.

Usage Example

// Typical usage: set the multiplier on the prefab in editor or at runtime, then let the mod/mode system call ApplyModeData.
// Example of directly scheduling the mode job (if needed programmatically):

var mode = /* reference to CrimeAccumulationGlobalMode prefab */;
mode.m_CrimeRateMultiplier = 0.75f; // reduce crime rates to 75%

EntityQuery query = entityManager.CreateEntityQuery(mode.GetEntityQueryDesc());
JobHandle handle = mode.ApplyModeData(entityManager, query, default);
handle.Complete(); // wait for completion if synchronous behavior is required

Additional notes: - Tweak m_CrimeRateMultiplier with care: values less than 0 are not validated in this class and may produce unintended results. - Because this operates on raw component data in jobs, do not perform managed-object access inside the job. The code adheres to that by only reading/writing native component data.