Server calling events on destroyed objects, even after unsubscribing.

I have an event which is subscribed to by the server when Attached, and unsubscribed when destroyed(I've tried Detached as well). However, even after that object is removed from the server using BoltNetwork.Destroy(), the event is still trying to call on the destroyed object which results in a null/missing object error.

Destroy code
public override void Disconnected(BoltConnection connection)
    {
        if (!BoltNetwork.IsServer)
            return;

        //Find all objects for the leaving connection.
        List<GameObject> connectionObjects = new List<GameObject>();
        foreach (BoltEntity item in BoltNetwork.Entities)
        {
            if (item.Controller == connection)
                connectionObjects.Add(item.gameObject);
        }
        //Destroy found objects.
        for (int i = 0; i < connectionObjects.Count; i++) 
            BoltNetwork.Destroy(connectionObjects[i]);
    }

Event code
    public override void Attached()
    {
        if (BoltNetwork.IsServer)
        {
            BoltNetwork.AddGlobalEventCallback<PlayerSetActiveEvent>(OnPlayerSetActiveEvent);
            BoltNetwork.AddGlobalEventCallback<GlobalDeathEvent>(OnGlobalDeathEvent);
        }
        if (BoltNetwork.IsClient && !base.entity.HasControl)
            base.state.AddCallback("PEnabled", OnStateEnabled);
}

    private void OnDestroy()
    {
        if (BoltNetwork.IsServer)
        {
            BoltNetwork.RemoveGlobalEventCallback<PlayerSetActiveEvent>(OnPlayerSetActiveEvent);
            BoltNetwork.RemoveGlobalEventCallback<GlobalDeathEvent>(OnGlobalDeathEvent);
        }

        if (BoltNetwork.IsClient && !base.entity.HasControl)
            base.state.RemoveCallback("PEnabled", OnStateEnabled);
    }

Comments

  • Scenario:
    - PlayerA spawns in.
    - Server subscribes to events.
    - PlayerA quits.
    - Server calls Destroy and unsubscribe is triggered via OnDestroy(or Detached).
    - Next time event is called, it also calls on destroyed object which should have been unsubscribed.
  • Hey, how do you get ur events to be called in response to the raised Bolt event?
Sign In or Register to comment.