WxAaRoNxW
WxAaRoNxW
Explore posts from servers
CC#
Created by WxAaRoNxW on 10/15/2023 in #help
❔ Am I doing event subscription correctly?
I have 2 singleton classes SingletonA, working mostly alone and has events and the other, SingletonB can also work alone but in my code, needs to rely on the A's events so that it functions. both are initialized at the same time, both are uninitialized at the same time on initialization, SingletonB subscribes to SingletonA and on uninitialization SingletonB unsubscribes to SingletonA
// SingletonB
public void EnableClass()
{
TrackingAssistant.Instance.OnTrackedObjectAdd += OnTrackedObjectAdd;
TrackingAssistant.Instance.OnTrackedObjectRemove += OnTrackedObjectRemove;
TrackingAssistant.Instance.OnTrackedObjectUpdate += OnTrackedObjectUpdate;
TrackingAssistant.Instance.OnTrackedObjectsReload += OnTrackedObjectsReload;
}

public void DisableClass()
{
TrackingAssistant.Instance.OnTrackedObjectAdd -= OnTrackedObjectAdd;
TrackingAssistant.Instance.OnTrackedObjectRemove -= OnTrackedObjectRemove;
TrackingAssistant.Instance.OnTrackedObjectUpdate -= OnTrackedObjectUpdate;
TrackingAssistant.Instance.OnTrackedObjectsReload -= OnTrackedObjectsReload;
}
// SingletonB
public void EnableClass()
{
TrackingAssistant.Instance.OnTrackedObjectAdd += OnTrackedObjectAdd;
TrackingAssistant.Instance.OnTrackedObjectRemove += OnTrackedObjectRemove;
TrackingAssistant.Instance.OnTrackedObjectUpdate += OnTrackedObjectUpdate;
TrackingAssistant.Instance.OnTrackedObjectsReload += OnTrackedObjectsReload;
}

public void DisableClass()
{
TrackingAssistant.Instance.OnTrackedObjectAdd -= OnTrackedObjectAdd;
TrackingAssistant.Instance.OnTrackedObjectRemove -= OnTrackedObjectRemove;
TrackingAssistant.Instance.OnTrackedObjectUpdate -= OnTrackedObjectUpdate;
TrackingAssistant.Instance.OnTrackedObjectsReload -= OnTrackedObjectsReload;
}
but there are times when SingletonA unitializes first so the Instance reference is set to null leaving SingletonB's uninitialization throwing an exception. do I just slap a null condition before unsubscribing or is there a better way?
4 replies