ObjectSpawner - jimdroberts/FishMMO GitHub Wiki
ObjectSpawner is a networked MonoBehaviour for FishMMO that manages spawning and respawning of networked objects in the game world. It supports various spawn types (linear, random, weighted), respawn conditions, object pooling, and flexible spawn position logic, enabling dynamic and efficient population of the game world with entities.
-
[HideInInspector] public Transform Transform
Cached reference to the spawner's transform.
-
public List OrConditions
If any of these conditions return true, the object will respawn. This list is checked first (logical OR).
-
public List TrueConditions
All conditions must return true for the object to respawn. This list is checked second (logical AND).
-
[HideInInspector] public bool IsCacheDirty
Flag to track if the spawn chance cache needs updating.
-
public float InitialRespawnTime
The initial respawn time (in seconds) for spawned objects.
-
public int InitialSpawnCount
The number of objects to spawn initially when the spawner starts.
-
[Tooltip] public int MaxSpawnCount
The maximum number of objects that can be spawned by this spawner.
-
public ObjectSpawnType SpawnType
The type of spawn selection (Linear, Random, Weighted).
-
[Tooltip] public bool RandomRespawnTime
If true, a random respawn time is selected within the minimum and maximum range. Otherwise, the initial respawn time is used.
-
[Tooltip] public bool RandomSpawnPosition
If true, a random spawn position is picked inside the bounding box using the current position as the center.
-
[Tooltip] public float SphereRadius
SphereCast radius used for spawning objects in the world.
-
public Vector3 BoundingBoxSize
The size of the bounding box used for random spawn position selection.
-
[HideInInspector] public Vector3 BoundingBoxExtents
The extents (half-size) of the bounding box, calculated from BoundingBoxSize.
-
public List Spawnables
The list of spawnable settings used to configure each spawnable object.
-
public Dictionary<long, ISpawnable> Spawned
Dictionary of currently spawned objects, keyed by their unique ID.
-
public List SpawnableRespawnTimers
List of respawn timers for each spawnable object.
#if UNITY_EDITOR
-
public Color GizmoColor
The color used to draw the spawner's gizmo in the editor. #endif
-
public override void OnStartNetwork()
Called when the network starts. Initializes spawner, validates spawnables, and spawns initial objects.
#if UNITY_EDITOR
-
void OnDrawGizmos()
Draws the spawner's bounding box or collider gizmo in the editor for visualization. #endif
-
public void Despawn(ISpawnable spawnable)
Despawns the specified spawnable object, schedules its respawn, and removes it from the spawned dictionary. Parameters: - ISpawnable spawnable: The spawnable object to despawn. Returns: void. No exceptions are thrown.
-
private DateTime GetNextRespawnTime(SpawnableSettings spawnableSettings)
Calculates the next respawn time for a spawnable object based on its settings and spawner configuration. Parameters: - SpawnableSettings spawnableSettings: The settings for the spawnable object. Returns: DateTime. The time when the object should respawn. No exceptions are thrown.
-
public void TryRespawn()
Attempts to respawn objects if their timers have elapsed and respawn conditions are met.
-
private void UpdateTotalSpawnChanceCache()
Updates the cached total spawn chance for weighted spawn selection. Only recalculates if cache is dirty.
-
private int GetWeightedSpawnIndex()
Selects a spawnable index based on weighted random selection using spawn chances. Returns: int. The index of the selected spawnable.
-
public int GetSpawnIndex()
Gets the index of the next spawnable to use, based on the configured spawn type. Returns: int. The index of the selected spawnable.
-
public void SpawnObject()
Spawns a new object in the world using the selected spawnable settings and position logic.
- Add the ObjectSpawner component to a GameObject in your scene.
- Configure the Spawnables list with the desired spawnable settings.
- Set MaxSpawnCount, InitialSpawnCount, and other fields as needed.
- Optionally, add respawn conditions to OrConditions and TrueConditions lists.
// Example 1: Setting up an ObjectSpawner in the Unity Editor
// 1. Add ObjectSpawner to a GameObject.
// 2. Configure Spawnables, MaxSpawnCount, and other fields in the inspector.
// 3. Add respawn conditions as needed.
// Example 2: Spawning and despawning objects in code
objectSpawner.SpawnObject();
objectSpawner.Despawn(mySpawnableEntity);
- Use respawn conditions to control when and how objects are respawned for dynamic gameplay.
- Set RandomSpawnPosition and BoundingBoxSize to distribute spawns within an area.
- Use object pooling for efficient networked object management.
- Document the purpose and configuration of each ObjectSpawner for maintainability.