NetworkPrefab
A class in Root.Township, Alta.Networking.
Root component of a spawnable networked prefab. It holds the prefab hash used to spawn and save it, its embedded child entities, and how it saves (SaveMode). Sits next to the root NetworkEntity; reach it with entity.Prefab or PrefabRoot.Prefab.
- Listed in Networking and servers
- Inherits or implements
NetworkEntityParent,INetworkEntityBehaviour,ISerializeChildren - Derived types
TypeSerializationHelper.NetworkPrefabSerialization - Nested types
ChildPrefab - Attributes
TypeConverter,DisallowMultipleComponent,SelectionBase - Documented in Prefabs and prefab hashes, Arguments and results
Constructors
| Member | What it is |
|---|---|
NetworkPrefab() | Unity makes components itself; use AddComponent or spawn a prefab instead of new. |
Fields
12 private and internal fields
| Member | What it is |
|---|---|
private List<int> callBackOrder | Order in which save indices were registered through GetSpawnedChildren; callbacks run in this order after loading. |
private bool canBeSpawnedThroughCommand | Backing field for CanBeSpawnedThroughCommand; cleared by DisableSpawnThroughCommands. |
private int childCount | Backing field for the child count reported through ISerializeChildren. |
private static List<NetworkPrefab.ChildPrefab> childPrefabAccumulation | Static scratch list used while child prefabs are gathered for saving or loading. |
private List<NetworkPrefab.ChildPrefab> childPrefabs | Backing field for the child prefab list reported through ISerializeChildren; rebuilt on each save. |
private Dictionary<int, List<NetworkPrefabChildEventHandler>> childrenCallback | Callbacks registered through GetSpawnedChildren, keyed by save index. |
private Dictionary<uint, NetworkEntity> embeddedHashes | Embedded entities keyed by their hash; used by GetEmbedded and when loading saved embedded entities. |
private HashSet<int> emptyIndices | Save indices marked empty with AddEmptyChildAtIndex, applied by ProcessEmptyIndices. |
private NetworkEntity entity | The NetworkEntity on the same object; backing field for Entity. |
private int hash | Backing field for Hash, the prefab hash used to spawn and save it. Validated in OnValidate. |
private static readonly Logger logger | NLog logger for this class. |
private PositionSyncBlocker positionSyncLock | Flags of the blockers currently stopping position sync; IsSyncingPosition is true when none are set. |
Properties
| Member | What it is |
|---|---|
bool CanBeSpawnedThroughCommand { get; } | Whether the prefab shows up for the spawn console command. |
NetworkEntity Entity { get; } | The root NetworkEntity of the prefab. |
uint Hash { get; } | The prefab's unique hash. This is what spawn messages, saves and PrefabManager use to identify it. |
bool IsServerOnly { get; set; } | Whether the prefab and its embedded entities are hidden from clients. Setting it true despawns them for every player; false spawns them for players in the chunk. |
bool IsSyncingPosition { get; } | False while any position sync blocker is set (for example while docked), so position updates aren't sent. |
SaveMode SaveMode { get; set; } | How this prefab saves: on its own (Base), as part of its parent (Child), not at all (Ignore), or with a player or creature. |
static int SpawnedCount { get; private set; } | Static count of prefab instances currently alive. |
2 private and internal properties
| Member | What it is |
|---|---|
private int Alta.Networking.ISerializeChildren.ChildCount { get; } | Number of child prefabs found for the save in progress. |
private List<NetworkPrefab.ChildPrefab> Alta.Networking.ISerializeChildren.ChildPrefabs { get; } | Child prefabs (with their parents) found for the save in progress. |
Events
| Member | What it is |
|---|---|
event NetworkPrefabEventHandler PreDestroyed | Raised just before the prefab is destroyed. |
Methods
| Member | What it is |
|---|---|
void DisableSpawnThroughCommands() | Hides this prefab from the spawn command. |
T GetComponentInPrefab<T>() | First component of that type on the root entity or any embedded entity, without looking into separate child prefabs. |
IEnumerable<T> GetComponentsInPrefab<T>() (iterator) | All components of that type on the root entity and its embedded entities. |
NetworkEntity GetEmbedded(uint hash) | Finds an embedded entity by its hash, or null. |
void Initialize() | Rebuilds the embedded entity hash lookup. |
void InitializeAsServer(SpawnData spawnData) | Does nothing. |
void InitializeChildren(uint[] embeddedIdentifiers) | Registers and initialises the embedded entities. The server assigns new ids; a client uses the ids sent in the spawn message. |
void InitializeChildrenBehaviours() | Initialises the behaviours on every embedded entity. |
void LockPositionSync(PositionSyncBlocker blocker) | Adds a reason to stop syncing this prefab's position. |
void OnConnected(Player target) | Server: tells a newly connected player about embedded entities that have been destroyed. |
void OnDisconnected(Player target) | Does nothing. |
override void OnValidate() | Makes sure the prefab has a valid hash. |
void PreDestroy() | Raises PreDestroyed, then detaches the entity from its parent and chunk. |
void SaveSerialize(Stream stream, ChunkVersioning chunkVersioning) | Writes or reads the prefab's full save: its savable components, embedded entities and child prefabs with SaveMode.Child. |
static void SaveSerializeEmpty(Stream stream, ChunkVersioning chunkVersioning) | Reads past a saved prefab without loading it, used when the saved prefab no longer exists. |
void UnlockPositionSync(PositionSyncBlocker blocker) | Removes a reason added with LockPositionSync. |
void ValidateEmbeddedHashes() | Empty in this build. |
protected override void UpdateEntityEmbeddedIds() | Refreshes the embedded entity ids and the hash to entity lookup. |
13 private and internal methods
| Member | What it is |
|---|---|
private void AccumulateChildPrefabs(Chunker parent) | Walks the entity's chunking children and collects every child prefab whose SaveMode is Child, so they are saved with this prefab. |
private void Alta.Networking.ISerializeChildren.AddChild(NetworkEntity parent, NetworkPrefab child) | Adds a child prefab and its parent to the list being saved; PositionRestrictor uses it to save items it holds. |
private void Alta.Networking.ISerializeChildren.AddEmptyChildAtIndex(int index) | During save loading, marks a child slot as empty so later child indices still line up; used by PickupDock. |
private void Awake() | Raises SpawnedCount and links each embedded entity to this prefab's entity as its parent. |
private int Alta.Networking.ISerializeChildren.GetChildIndex(NetworkPrefab child) | Returns the save index of a child prefab, or of the first child under a given parent entity, or -1. Lets behaviours save which child is theirs. |
private int Alta.Networking.ISerializeChildren.GetChildIndex(NetworkEntity parent) | Returns the save index of a child prefab, or of the first child under a given parent entity, or -1. Lets behaviours save which child is theirs. |
private void Alta.Networking.ISerializeChildren.GetSpawnedChildren(int index, NetworkPrefabChildEventHandler callback) | Registers a callback that runs once the child at a save index has been loaded; used by docks, populations and buildups to find their saved children again. |
private void OnDestroy() | Lowers SpawnedCount. |
private void ProcessEmptyIndices() | Inserts null slots for indices marked with AddEmptyChildAtIndex so loaded children match their saved indices. |
private void Reset() | Editor reset; runs OnValidate to give the prefab a hash. |
private static void SaveSerializeEmpty(Stream stream) | Reads past a saved prefab without loading it, used when the saved prefab no longer exists. |
private void SaveSerializeEntity(Stream stream, ChunkVersioning chunkVersioning, uint hash) | Saves or loads one embedded entity by hash, writing its size first so a missing entity's data can be skipped on load. |
private void SaveSerializeEntity(Stream stream, ChunkVersioning chunkVersioning, NetworkEntity entity) | Saves or loads one embedded entity by hash, writing its size first so a missing entity's data can be skipped on load. |
Used in
Members of other types that take, return or hold a NetworkPrefab:
Smelter.MarkPlayersAction, ChisellingDeck.blockMap, ChisellingDeck.BuilderLevel.blockToSpawn, ChisellingDeck.BuilderLevel.BlockToSpawn, CaveTeleportSettings.fuelPrefab, CaveTeleportSettings.FuelPrefab, ConstrainedSpawnerDisplay.prefab, BasicObjectDisplay.prefabs, GeneralSettings.layerPrefab, GeneralSettings.LayerPrefab, StreamingSettings.caveStreamerPrefab, StreamingSettings.CaveStreamerPrefab, StreamingSettings.SpecialLayer.prefab, StreamerHelper.SerializePrefab, StreamerHelper.WritePositionlessPrefab, DebuggingCommandModule.Count, PrefabInfoDump.op_Implicit, RepairBoxModule.SpawnBox, SelectionCommandModule.FindPrefab, SpawnCommandModule.For, SpawnCommandModule.Spawn, SpawnCommandModule.SpawnAtPosition, SpawnCommandModule.SpawnFull, SpawnCommandModule.SpawnLocal, SpawnCommandModule.Moulds.DropPackage, TradeCommandModule.PostBox, WackyCommandModule.DestroyAll, WackyCommandModule.DestroyBase, HashedItemInfo.op_Implicit, Cookable.burntVersion, Cookable.cookedVersion, CookingSettings.nonRecipeStewPrefab, CookingSettings.NonRecipeStewPrefab, GenericCraftingRecipe.GetRecipe, ForestPopulationCreator.prefab, PerChunkForestSpawner.prefab, CutableObject.prefabToSpawnOnHit, SeedSprayManager.prefab, Item.Prefab, Lock.correctKeyPrefab, LockableHingeSaveData.lockPrefab, PersonalChunk.prefab, PickupDock.LoadDockPickup, DebugPackage.PrefabQuantity.prefab, DebugPackage.PrefabQuantity.Prefab, DebugPackage.PrefabQuantity.PrefabQuantity, ClientSpawnManager.CheckSpawnEvent, ClientSpawnManager.SpawnExisting, INetworkEntity.Prefab, ISerializeChildren.AddChild, ISerializeChildren.GetChildIndex, NetworkEntity.prefab, NetworkEntity.Prefab, NetworkEntity.SpawnAndReplaceWith, NetworkPrefab.ChildPrefab.child, NetworkPrefab.ChildPrefab.ChildPrefab, NetworkPrefabChildEventHandler.BeginInvoke, NetworkPrefabChildEventHandler.Invoke, NetworkPrefabEventHandler.BeginInvoke, NetworkPrefabEventHandler.Invoke and 127 more.