codeAPI Reference

Global.ISpawnEvents Spawn Event Interface

calendar_today May 3, 2026 schedule ~1 min read person patrickjr verified 50

Global.ISpawnEvents Interface

Scene-wide event interface for listening to spawn operations. Implement on components to receive pre/post spawn callbacks.

Interface Definition

CSHARP
public static partial class Global
{
    public interface ISpawnEvents : ISceneEvent<ISpawnEvents>
    {
        /// <summary>
        /// Data passed to OnSpawn. Set Cancelled to true to prevent the spawn.
        /// </summary>
        public class SpawnData
        {
            /// <summary>
            /// The spawner that will create the object(s).
            /// </summary>
            public ISpawner Spawner { get; init; }

            /// <summary>
            /// The world-space transform where the object will be placed.
            /// </summary>
            public Transform Transform { get; init; }

            /// <summary>
            /// The player requesting the spawn.
            /// </summary>
            public PlayerData Player { get; init; }

            /// <summary>
            /// Set to true to cancel the spawn.
            /// </summary>
            public bool Cancelled { get; set; }
        }

        /// <summary>
        /// Data passed to OnPostSpawn after a successful spawn.
        /// </summary>
        public class PostSpawnData : SpawnData
        {
            /// <summary>
            /// The GameObjects that were spawned.
            /// </summary>
            public List<GameObject> Objects { get; init; }
        }

        /// <summary>
        /// Called before an object is spawned into the world.
        /// Set SpawnData.Cancelled to true to reject the spawn.
        /// </summary>
        void OnSpawn(SpawnData e) { }

        /// <summary>
        /// Called after an object has been successfully spawned into the world.
        /// </summary>
        void OnPostSpawn(PostSpawnData e) { }
    }
}

Usage Example

CSHARP
public class SpawnLimiter : Component, Global.ISpawnEvents
{
    [Property] public int MaxSpawnsPerMinute { get; set; } = 10;
    
    private Queue<float> _recentSpawns = new();

    void Global.ISpawnEvents.OnSpawn(Global.ISpawnEvents.SpawnData e)
    {
        // Clean up old entries
        while (_recentSpawns.Count > 0 && _recentSpawns.Peek() < Time.Now - 60f)
            _recentSpawns.Dequeue();
        
        if (_recentSpawns.Count >= MaxSpawnsPerMinute)
        {
            e.Cancelled = true;
            Log.Info("Spawn rate limit exceeded");
        }
    }

    void Global.ISpawnEvents.OnPostSpawn(Global.ISpawnEvents.PostSpawnData e)
    {
        _recentSpawns.Enqueue(Time.Now);
    }
}

Key Behaviors

Was this helpful?