Table of Contents

Class SpawnerPool

Namespace
FishMMO.Server.Implementation.World.SceneServer.Spawner
Assembly
FishMMO.Server.dll

Pre-allocates the network objects a scene will need, so a map's memory footprint is fixed at load rather than discovered under load.

public static class SpawnerPool
Inheritance
SpawnerPool
Inherited Members

Remarks

FishNet's FishNet.Utility.Performance.DefaultObjectPool already recycles despawned objects, so nothing was leaking — but it fills lazily. The first time a spawner needs a prefab it instantiates one, which means a freshly loaded map pays for every NPC it will ever show as players walk into each spawner's range: hitching during play, and a heap that only reaches its true size once the whole map has been visited. Neither is acceptable to plan capacity against.

Reserving up front converts that into a known, one-time load cost. It also removes the pathological case where a spike in concurrent spawns instantiates a batch that then sits in the pool forever: the reservation is the budget, and MaxSpawnCount caps what any spawner can draw.

The pool outlives the scenes. A pooled instance is kept out of every world scene (Keep(NetworkManager, NetworkObject)): FishNet leaves a despawned object wherever it was, and a spawned one is moved into its world scene, so every instance that had ever been used died with that scene when it unloaded, while the reservation still counted it. The next instance of a dungeon then saw no shortfall, skipped the prewarm and instantiated everything lazily in play. Now only what was alive at the unload goes with the scene, and Forget(NetworkObject, int) takes exactly that out of the count, so the next load makes it again up front. The rule itself lives in Shared, because the spawner-less despawns — a corpse with no spawner, ground loot — are Shared code and pool the same way.

Properties

TotalReserved

Total objects reserved across every prefab, for diagnostics.

public static int TotalReserved { get; }

Property Value

int

Methods

Clear()

Forgets all reservations. Call when tearing a scene server down so a subsequent load re-reserves against a fresh pool.

public static void Clear()

Forget(NetworkObject, int)

Takes instances that no longer exist out of a prefab's reservation.

public static void Forget(NetworkObject prefab, int count)

Parameters

prefab NetworkObject

The prefab the lost instances were made from.

count int

How many were lost.

Remarks

Called for each object a spawner still had alive when its scene unloaded: those went with the scene. The next spawner to reserve the prefab then sees the shortfall and makes them again at its load, instead of the pool finding itself empty during play. Clamped at zero, since the pool also grows on demand past the reservation and an instance made that way can be among those lost.

LogReservation(string)

Logs the reservation total. Called once after a scene's spawners have initialised.

public static void LogReservation(string context)

Parameters

context string

Scene or spawner description for the log line.

Reserve(NetworkManager, NetworkObject, int)

Ensures the pool holds at least count instances of a prefab.

public static int Reserve(NetworkManager networkManager, NetworkObject prefab, int count)

Parameters

networkManager NetworkManager

The network manager owning the pool.

prefab NetworkObject

The prefab to reserve instances of.

count int

How many instances should exist.

Returns

int

The number of instances newly created.

Remarks

Idempotent per prefab: reserving 5 and then 8 instantiates 5 and then 3, never 13. That matters because several spawners commonly share one prefab, and summing their maxima would multiply the reservation for no benefit — a prefab's peak concurrent count is bounded by the largest single demand plus whatever else is live, and the pool grows on demand beyond the reservation anyway.