Skip to main content

PopulationSample

A class in Root.Township, Alta.Populations.

Server: one population (creatures or items of one PopulationDefinition) in one chunk. It ticks on the timeline to grow the population count towards its maximum, spawns members at free spawn points while the chunk is loaded, and tracks deaths.

Constructors​

MemberWhat it is
PopulationSample(PopulationDefinition definition, int currentPopulation, int maximumPopulation, PopulationFolderBase parent, bool isOneOff = false)Creates a PopulationSample from definition, currentPopulation, maximumPopulation, chunk, spawner, parent and isOneOff.
PopulationSample(PopulationDefinition definition, int currentPopulation, int maximumPopulation, LocationChunk chunk, ISpawnSystem spawner, PopulationFolderBase parent, bool isOneOff = false)Creates a PopulationSample from definition, currentPopulation, maximumPopulation, chunk, spawner, parent and isOneOff.

Fields​

MemberWhat it is
PopulationDelegate OnPopulationDiedOutCalled when the count reaches 0.
PopulationDelegate OnPopulationMaxedCalled when the count reaches the maximum.
PopulationDelegate OnPopulationStartedCalled when the count goes from 0 to above 0.
4 private and internal fields
MemberWhat it is
private Timeline.IEntry entryTimeline entry for the repeating population check. Saved and loaded with the sample.
private bool isOneOffTrue for a one-off spawn: no repeating tick and spawned entities are not tracked.
private static Logger loggerNLog logger for this class.
private ISpawnSystem spawnerSpawn system that provides spawn points for this population.

Properties​

MemberWhat it is
LocationChunk Chunk { get; private set; }The location chunk the population lives in.
int CurrentPopulation { get; private set; }Population count, including members not currently spawned.
PopulationDefinition Definition { get; }The population definition.
uint Hash { get; }Hash of the population definition.
bool IsInitialized { get; private set; }True after Initialize.
int MaximumPopulation { get; private set; }Largest population count.
PopulationFolderBase Parent { get; }The population folder that owns this sample. Non-creature members are chunked under its entity.
Dictionary<NetworkEntity, PopulationItem> SpawnedEntities { get; private set; }Spawned members, with their PopulationItem components.

Events​

MemberWhat it is
event PopulationItemSpawnHandler NewEntitySpawnedRaised when a member is spawned.

Methods​

MemberWhat it is
void AddEntity(NetworkEntity entity, int pointIndex)Adds an existing entity as a member at the spawn point with the given index.
void CheckSpawned()Spawns members until the spawned count matches the population times the definition's spawned percentage. Gives up after three failed spawns.
void Destroy()Stops the population's timer.
void ForceKillAll()Destroys every spawned member, sets the population to 0 and rechecks spawning.
void ForceSpawn(int targetPopulation)Raises the population to the target (capped at the maximum) and spawns to match.
void Initialize(LocationChunk chunk, ISpawnSystem spawner)Server: sets the chunk and spawner and starts the growth tick.
void LoadTime(TimeSpan time)Restores the tick timer from a save and runs the ticks missed while unloaded.
void LoseMember(NetworkEntity entity)Removes a member that died, occupies its spawn point for a while and lowers the count.
void Replace(PopulationItem populationItem, NetworkEntity entity)Swaps a member's entity for a new one at the same spawn point.
bool RequestMove(NetworkEntity entity, bool isAllowingSame = true)Moves a member to another random free spawn point. False if none is available.
TimeSpan SaveTime()Returns the tick timer for saving.
void SerializeTime(Stream stream)Obsolete: old way of saving the tick timer to a stream.
9 private and internal methods
MemberWhat it is
private void AddEntity(NetworkEntity entity, SpawnPoint point)Adds an existing entity as a member at the spawn point with the given index.
private PopulationItem AddItemTo(GameObject gameObject)Adds the right PopulationItem component to a spawned entity: radial, bounds or plain, by the definition's disconnect settings.
private void CheckPopulation()Server tick: shrinks an over-max population or grows it by the growth curve, then spawns entities if the chunk is loaded.
private void CheckTimeUpdated(PopulationDefinition changedPopulation)Reschedules the population tick when the definition's tick frequency is changed.
private void DespawnEntity(NetworkEntity entity, bool isDeath)Despawns a tracked entity and removes it from SpawnedEntities.
private void DespawnEntityInternal(NetworkEntity entity, bool isDeath)Unhooks removal events, frees or occupies the spawn point (on death) and destroys the population item.
private void EntityDestroyed(NetworkEntity entity, bool isUnload)Entity destroyed handler: an unload just despawns it, a real destroy counts as losing a member.
private void RegisterRemoveMethod(NetworkEntity newEntity)Hooks the entity's removal methods and its destroyed event so the sample notices when it leaves.
private bool SpawnRandom(PopulationSpawnInformation value)Server: spawns one random prefab at a free spawn point, applying the definition's rotation, safe spawn and chunk rules.

Used in​

Members of other types that take, return or hold a PopulationSample: