Game.RandomLocalizationInitializeSystem
Assembly: Game
Namespace: Game.Common
Type: class
Base: GameSystemBase
Summary:
System that initializes per-entity RandomLocalizationIndex buffers when entities are created. It queries for entities that have the Created tag, a RandomLocalizationIndex buffer and a PrefabRef, and excludes Temp and OutsideConnection. For each matching entity it looks up localization counts (either directly on the prefab or via SpawnableBuildingData → zone prefab), seeds a Random instance using a global RandomSeed and the entity index, and generates random localization indices into the entity's RandomLocalizationIndex buffer via RandomLocalizationIndex.GenerateRandomIndices. The work is done in a Burst-compiled IJobChunk (InitializeLocalizationJob) and scheduled in parallel.
Fields
-
private Unity.Entities.EntityQuery m_CreatedQuery
Query used to find entities that require localization index initialization. It matches entities with the Created component, a RandomLocalizationIndex buffer and a PrefabRef, and excludes Temp and Game.Objects.OutsideConnection. This query is required for update of the system. -
private TypeHandle __TypeHandle
Internal struct instance that caches and assigns various Entity/Component/Buffer type handles and lookups (EntityTypeHandle, ComponentTypeHandle, BufferTypeHandle , BufferLookup , ComponentLookup ) for use when constructing the job.
Properties
- No public properties.
(The system uses internal type handles and local job data fields; there are no exposed properties on this system class.)
Constructors
public RandomLocalizationInitializeSystem()
Default constructor (marked with [Preserve]). No special runtime initialization beyond what the base GameSystemBase does. The real initialization of queries and handles occurs in OnCreate / OnCreateForCompiler.
Methods
-
protected override void OnCreate()
Creates and stores m_CreatedQuery to match Created + RandomLocalizationIndex buffer + PrefabRef, excluding Temp and OutsideConnection. Calls RequireForUpdate(m_CreatedQuery) so the system only runs when matching entities exist. -
protected override void OnUpdate()
Builds an InitializeLocalizationJob instance by obtaining concrete Entity/Component/Buffer handles (via InternalCompilerInterface.Get* wrappers and the cached __TypeHandle entries), sets m_RandomSeed = RandomSeed.Next(), and schedules the job with JobChunkExtensions.ScheduleParallel against m_CreatedQuery. The scheduled job fills RandomLocalizationIndex buffers for matching entities in parallel. -
protected override void OnCreateForCompiler()
Called by the managed/IL2CPP compiler support; ensures __AssignQueries is invoked and type handles are assigned (__TypeHandle.__AssignHandles). Keeps data consistent with the code-generated type-handle plumbing. -
private void __AssignQueries(ref SystemState state)
Small helper used at compile-time / runtime initialization; currently constructs and disposes a temporary EntityQueryBuilder (keeps compile-time compatibility). Marked aggressively inlined. -
(Nested)
private struct InitializeLocalizationJob : IJobChunk
- Fields: ReadOnly EntityTypeHandle m_EntityType; ReadOnly ComponentTypeHandle
m_PrefabRefType; BufferTypeHandle m_RandomLocalizationIndexType; ReadOnly BufferLookup m_LocalizationCounts; ReadOnly ComponentLookup m_SpawnableBuildingData; RandomSeed m_RandomSeed. - Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask): iterates chunk entities, reads prefab (PrefabRef.m_Prefab), obtains the RandomLocalizationIndex buffer for the entity and tries to get localization counts for the prefab (via TryGetLocalizationCount). If counts are found, creates a Random seeded by m_RandomSeed.GetRandom(entity.Index + 1) and calls RandomLocalizationIndex.GenerateRandomIndices(indices, counts, ref random) to populate indices.
-
private bool TryGetLocalizationCount(Entity prefab, out DynamicBuffer
counts): attempts to get localization counts buffer directly for the prefab; if not present, and if the prefab has SpawnableBuildingData, it tries to get the counts buffer from componentData.m_ZonePrefab. Returns true if counts found. -
(Explicit interface implementation)
void IJobChunk.Execute(...)
For IJobChunk interface compliance; forwards to the strongly-typed Execute method.
Usage Example
// Create an entity that will get random localization indices initialized by this system.
// After creating an entity with Created + PrefabRef + RandomLocalizationIndex buffer,
// RandomLocalizationInitializeSystem will run (when the Created tag is present) and fill the buffer.
Entity prefabEntity = /* existing prefab entity reference */;
EntityManager em = World.DefaultGameObjectInjectionWorld.EntityManager;
Entity e = em.CreateEntity();
em.AddComponentData(e, new Created()); // marks the entity as newly created so the system will process it
em.AddComponentData(e, new PrefabRef { m_Prefab = prefabEntity });
em.AddBuffer<RandomLocalizationIndex>(e);
// On the next update (or same frame depending on scheduling), RandomLocalizationInitializeSystem's job
// will find this entity, look up LocalizationCount for the prefab (or zone prefab via SpawnableBuildingData),
// seed a Random using RandomSeed.Next() and entity.Index, and call
// RandomLocalizationIndex.GenerateRandomIndices(buffer, counts, ref random) to populate the buffer.