Skip to main content

World, entities and spawning

Everything you can see, hold or fight in A Township Tale is a networked object called an entity. Entities live in chunks, chunks load around players, and a chunk saves its entities when it unloads. The forest, the caves, creatures, ore, loot and the day and night cycle are all built on top of that. This area covers those pieces, all in Root.Township.

The big picture​

  • An entity is a NetworkEntity. It has a runtime id (Identifier) that changes every time it spawns or loads. Its behaviours implement INetworkEntityBehaviour, usually by deriving from NetworkEntityBehaviour. See NetworkEntity.
  • A thing you can spawn is a NetworkPrefab. Each one has a fixed Hash (a uint). PrefabManager maps hashes and names to prefabs. A prefab can hold more entities inside it, called embedded entities. See Prefabs and prefab hashes.
  • Only the server spawns and destroys. It calls SpawnHelper (or SpawnManager), gives the new entity a chunk, and clients are told. See Spawning and Destroying and despawning.
  • The world is cut into chunks. A LocationChunk is a box in the world. On the server every entity players can see belongs to one chunk, or rides on another entity that does. There is also one global chunk and one void chunk. See Chunks.
  • Chunks load around players and save when they unload. Loading pulls in the chunk's static scenery, runs any procedural generation, then spawns its saved entities. See Chunk loading and saving.
  • Static scenery is not entities. Trees, rocks and buildings placed by the designers mostly come in as static content, with a few switchable "modifiable" objects. See Static objects.
  • Procedural parts are made from a seed. The forest (Forest) and the mine's cave layers (Caves) make their chunks from a seed. Creatures, ore and plants come from populations (Populations and spawners). Containers fill from loot tables (Loot).
  • Time runs on a timeline. Timeline drives everything that waits (respawns, decay, loot refills) and saves with the server. GameTimeManager turns it into a day and night. See Time and environment.

Who decides what​

WhatDecided byNotes
Spawning and destroying entitiesServerClientSpawnManager.SpawnInternal and Destroy only log an error.
Which chunk an entity is inServerClients don't keep chunk entity lists at all: Chunk.Entities and Chunk.Players are null there.
Which chunks loadServer, per playerPlayerChunkManager on the server works out the chunks around each player and tells their game which to load.
Where an object isMostly serverThe server moves loose objects and sends MoveEntity (ForcePositionSync). How held items and physics sync is in Entity sync and Items.
Where a player isThat player's clientThe server asks the client to move (PlayerEffectController.Teleport, LocomotionController.MoveTo). See Moving and teleporting, and Where is a player? to find out.
Forest and cave layoutServerClients download the forest's cache files from the server. For caves they get the seed and build the same rock themselves.
Populations, loot, ore respawnsServerAll run from Timeline callbacks on the server.
Time of dayServerIt syncs GameTimeManager every 10 seconds (the syncRefreshRate default). Clients ease towards it.
Sky, fog, ambienceClientAtmosphereManager and DayTimeManager do nothing visual on headless servers.

Threading​

Nearly all of it runs on Unity's main thread. Chunk loading, unloading and saving are async methods (Task), and they resume on the main thread through Unity's synchronization context, so you can touch game objects after an await that started on the main thread. A few parts of forest and cave generation run on worker threads (Task.Run) and only hand finished data back. Don't call game code from your own threads.

A note on the source​

The game ships two builds of Root.Township (both assembly version 0.0.39.2). The client download has the server code compiled out: in it NetworkScene.InitializeAsServer and ChunkFileHelper.GetFolder are empty, and ServerCacheManager, ServerWipeManager, WorldCommandModule and SavingCommandModule aren't there at all. Tavern servers run the server build, which TavernDefaults puts in place and att-stubs is made from. These pages were checked against that server build. Most world code is the same in both; where only the server build has something, the page says so.

The scenes​

The game's scenes, going by the scene dumps: level4 is the main world (one NetworkScene named "Scene 'A'" with about 4,160 embedded entities, the ForestGenerator, the CaveLayerManager under "Mines", and managers under "Managers"). level5 is the tutorial islands, with its own NetworkScene. level3 and level7 are menu scenes with a LocalNetworkScene. level6 is another world scene with chunks and caves but no ForestGenerator in its dump, and level8 a small scene with a ChunkDisplayTool, which looks like a test level. Mods that look for objects by name should check which scene they're in.

Quick recipes​

You want toUse
Spawn something in front of a playerPrefabManager.GetPrefab, LocationChunkHelper.GetBestContaining, SpawnHelper.Spawn (Spawning)
Spawn a stack of itemsSpawnHelper.SpawnPouchOrPickup
Remove an entityentity.PrefabRoot.SceneDestroy() on the server (Destroying)
Find everything near a pointWalk Chunk.ChunksByIndex and each chunk's Entities (Finding entities)
Count every entity by prefabEntityManager.IterateEntities, as Canary's census does
Know when an area is readyChunk.FullyLoaded, Chunk.IsFullyLoaded (Chunk loading and saving)
Keep an area loadedawait chunk.ForceLoad(), later chunk.IsForceLoaded = false
Move an objectSet its position, then ForcePositionSync() (Moving and teleporting)
Teleport a player far awayplayer.SafeMoveToChunk(chunk, position, callback)
React to nightGameTimeManager.DuskReached and DawnReached
Change how fast creatures or ore come backPopulationDefinitionManager.SetValue (Populations)

Pages in this area​

PageWhat's on it
NetworkEntityThe entity component: ids, lifecycle, behaviours, parents, events.
Prefabs and prefab hashesNetworkPrefab, PrefabManager, hashes, embedded entities, SaveMode.
SpawningSpawnHelper, SpawnManager, SpawnData, what the spawn command does.
Destroying and despawningSceneDestroy, destroy versus unload, entities that fall out of the world.
Finding entitiesEnumerating entities, by chunk, by id, by prefab. The select commands.
ChunksChunk, LocationChunk, the global and void chunks, Chunker, chunk lookup.
Chunk loading and savingHow chunks load, sync to players, unload and save.
Static objectsStatic scenery, modifiable and destroyable static objects, static instructions.
Moving and teleportingMoving objects and players, safely and across the world.
ForestThe procedural forest: ForestGenerator, its caches, its cost.
CavesCave layers, CaveLayerManager, CaveLayerStreamer, mine structures.
Populations and spawnersHow creatures, ore and plants spawn and respawn.
LootLoot tables, rarities, origins, containers that refill.
Time and environmentTimeline, TimeManager, GameTimeManager, sky and fog.
Map and landmarksThe map, map pieces and landmarks.

Messages, MethodSync and how entity state reaches clients are in Networking. Items, docks and pickups are in Items, interaction and damage. Creature AI is in Creatures, and console commands in general are in Console and utilities. The generated reference tables list every spawnable prefab with its hash.