0

I have a Register Callback method on a Game class that objects can register a callback with for when the game is shutting down.

public void RegisterAsyncShutdownCallback(Func<Task> callback)
{
    if (this.asyncShutdownCallbacks.Contains(callback))
    {
        return;
    }

    this.asyncShutdownCallbacks.Add(callback);
}

When the game shuts down, i invoke the callbacks.

public virtual async Task ShutdownGame()
{
    var callbackTasks = new List<Task>();
    foreach (var callback in this.asyncShutdownCallbacks)
    {
        callbackTasks.Add(callback());
    }

    // Invoke all of the asynchronous callbacks first.
    await Task.WhenAll(callbackTasks);
}

My question is, what if the owner of one of these callbacks becomes null, because the monster dies or the item was set to null etc. Am I holding a strong reference to the callback owner? If so, is it better practice to provide a unregister method or should I wrap them in a WeakReference?

The engine is using callbacks in place of events so I can perform async operations in player, monster, battle code etc. So a lot of my objects have this RegisterCallback style of method, and invokes callbacks when certain events happen within the object. If I am holding a strong reference, I'd prefer to hold a weak reference and self clean up rather than rely on objects properly unregistering. The engine provides an API that lets 3rd party users create plugins and register callbacks. I don't want to rely on the devs properly unregistering objects if I can control it within the engine itself.

Johnathon Sullinger
  • 7,097
  • 5
  • 37
  • 102
  • 2
    Yes, they’re strong references, and weak reference does not sound appropriate at all. Remove callbacks when removing objects from the world instead, for example. – Ry- Jan 04 '15 at 04:49
  • So I have to assume 3rd parties will unregister themselves properly from my code? I can add an interface to all my game objects that has a `Destroy()` method or something that allows it to unregister before it's set to null – Johnathon Sullinger Jan 04 '15 at 04:53
  • So a monster dies – how does that happen? There must already be a way to remove it from the world; unregister callbacks as part of that. – Ry- Jan 04 '15 at 04:56
  • Honestly I hadn't planned that far out. It makes sense to have a central place were objects registration to and unregistration is handled by – Johnathon Sullinger Jan 04 '15 at 05:02
  • I'm still building out the underlying pieces and haven't got to the actual creation of game objects, so I guess it's time to start figuring that piece out :) – Johnathon Sullinger Jan 04 '15 at 05:03
  • What happens if the dev registers an anonymous method? – Johnathon Sullinger Jan 04 '15 at 05:30
  • 4
    Sounds like you need to work with pen and paper rather than coding. You start coding when you have a general design of the complete project - what are the different classes and their relations and responsibilities etc. – SimpleVar Jan 04 '15 at 05:38

0 Answers0