Game.Policies.DefaultPoliciesSystem
Assembly:
Assembly-CSharp
Namespace:
Game.Policies
Type:
class
Base:
GameSystemBase
Summary:
DefaultPoliciesSystem is responsible for initializing default policy settings for newly created prefab instances and for the city configuration during deserialization (new games or older save conversions). It schedules a parallel IJobChunk (AddDefaultPoliciesJob) that scans entities marked as Created, finds DefaultPolicyData on the referenced prefab, and adds corresponding Policy entries to the instance's Policy buffer. It also implements IPostDeserialize.PostDeserialize to populate the city's Policy buffer from DefaultPolicyData when required (e.g., new games or backwards-compatibility loads). The system uses an internal TypeHandle to obtain component/buffer handles efficiently for job scheduling.
Fields
-
private CitySystem m_CitySystem
Reference to the CitySystem instance (obtained from the World). Used when populating the city's Policy buffer during PostDeserialize. -
private EntityQuery m_CreatedQuery
EntityQuery that matches entities with Created, Policy (buffer), and PrefabRef but excludes Temp. Used to drive the AddDefaultPoliciesJob scheduling. -
private EntityQuery m_CityConfigurationQuery
EntityQuery used to find the singleton/city configuration entity that can contain DefaultPolicyData (used in PostDeserialize). -
private TypeHandle __TypeHandle
Internal struct holding ComponentTypeHandle / BufferTypeHandle / ComponentLookup / BufferLookup used to build job data and to access components/buffers inside jobs. -
private struct AddDefaultPoliciesJob
(nested)
Job that runs over chunks of Created prefab instances and copies DefaultPolicyData (from the prefab) into the instance's Policy buffer. Checks for PolicySliderData to set slider defaults when available. -
private struct TypeHandle
(nested)
Holds read-only and read-write handles required by AddDefaultPoliciesJob. Provides __AssignHandles to bind handles from a SystemState.
Properties
- This type has no public properties.
(Internal handles and state are stored as private fields. The system exposes no public properties.)
Constructors
public DefaultPoliciesSystem()
Default parameterless constructor. The system is created by the ECS world; initialization happens in OnCreate.
Methods
-
protected override void OnCreate()
: System.Void
Initializes the system: retrieves the CitySystem, creates the EntityQueries used by the system (m_CreatedQuery and m_CityConfigurationQuery), and calls RequireForUpdate(m_CreatedQuery) so the system runs only when there are matching Created entities. Marked with [Preserve]. -
protected override void OnUpdate()
: System.Void
Builds an AddDefaultPoliciesJob with component/buffer lookups obtained via InternalCompilerInterface and the stored TypeHandle, then schedules it in parallel over m_CreatedQuery using JobChunkExtensions.ScheduleParallel and ties it into the system dependency (base.Dependency). -
public void PostDeserialize(Context context)
: System.Void
Implements IPostDeserialize. When deserializing for a new game or loading an old save (version check), it copies DefaultPolicyData entries from the city configuration singleton into the city's Policy buffer (m_CitySystem.City). For each DefaultPolicyData entry it looks up PolicySliderData on the policy prefab to set the slider default if available, otherwise it adds the policy with a 0.0f value. -
private void __AssignQueries(ref SystemState state)
: System.Void
Internal helper used by the compiler-generated OnCreateForCompiler; in this build it's effectively a placeholder (creates and disposes an EntityQueryBuilder). Used to ensure queries and handles are assigned in compiled form. -
protected override void OnCreateForCompiler()
: System.Void
Compiler helper that calls __AssignQueries and __TypeHandle.__AssignHandles to wire up handles for use by the job and the system when compiled with Burst/IL2CPP tooling. -
private struct AddDefaultPoliciesJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
: System.Void
The Execute method of the job: for each entity in the chunk, it reads the PrefabRef component, checks whether the referenced prefab contains a DefaultPolicyData buffer, and if so iterates that buffer adding Policy entries to the entity's Policy buffer. If a PolicySliderData component exists for the policy, it uses the slider's default value; otherwise it uses 0f. This method is scheduled in parallel from OnUpdate.
Usage Example
// Example: after this system has run (or after PostDeserialize), read the city's policies:
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
var citySystem = World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<Game.Policies.DefaultPoliciesSystem>(); // or obtain CitySystem directly
// Get the Policy dynamic buffer for the city entity (m_CitySystem.City)
DynamicBuffer<Policy> cityPolicies = entityManager.GetBuffer<Policy>(cityEntity);
for (int i = 0; i < cityPolicies.Length; i++)
{
Policy p = cityPolicies[i];
// Inspect p.m_Policy (entity/prefab reference), p.m_Flags, p.m_Value (slider/default)
}
Notes and implementation details: - The system relies on DefaultPolicyData buffers that live on prefabs (or the city configuration singleton). DefaultPolicyData contains references to Policy prefabs that should be applied by default. - When copying defaults the system prefers PolicySliderData.m_Default when the policy has an associated slider, otherwise it adds the policy with a 0f value. - The AddDefaultPoliciesJob uses ComponentTypeHandle/BufferTypeHandle/ComponentLookup/BufferLookup via an internal TypeHandle; these are bound each update through InternalCompilerInterface to be valid for burst-safe job execution.