A good Reference system can help your game

How many times has it happened where you have this one class, let’s say it’s a spaceship that was destroyed.

Now you have tell all other dependent objects that she died, any independent sprite animation class, AI, etc etc because they were relying on her. Another thing is, in my case, having a scene that contains all the objects and by deleting the ship It won’t get removed from the scene, keeping the reference alive and so Garbage collector won’t kick in. Deleting and removing from the list is a way, but why should you inside the spaceship class have know how to remove from the Scene list? Makes no sense. The list should know how to treat a deleted object.

I made a somewhat satisfactory solution.

A base class called Reference

   public class Reference
    {
        protected string    _name = "";
        protected bool      _notifying = false;
        protected   List _to_alert_of_death = new List();
 
        public Reference()
        {
            Name = "UnknownReference";
        }
 
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
 
        public void add_reference(Reference to_alert)
        {
            _to_alert_of_death.Add(to_alert);
        }
 
        public void remove_reference(Reference to_alert)
        {
            _to_alert_of_death.Remove(to_alert);
        }
 
        public void NotifyDeathToChilds()
        {
            // Prevents stack overflow if double reference exists
            if (_notifying) return;
            // Reference will be erase, notify children of death
            _notifying = true;
            for (int i = 0; i < _to_alert_of_death.Count; ++i)
            {
                _to_alert_of_death[i].notify_death(this);
            }
            _to_alert_of_death.Clear();
            _notifying = false;
        }
 
        public virtual void DeleteThis()
        {
            NotifyDeathToChilds();
        }
        public virtual void notify_death(Reference reference)
        {
            _to_alert_of_death.Remove(reference);
        }
    }

So when you create your dependent spaceship objects you just do something like:

Spaceship.add_reference(DepedentObject);

When you want to delete SpaceShip just do SpaceShip.DeleteThis(); and it will warn all dependent object that it’s being deleted

But how will the Dependent Object be aware of this?
Make it inherit from Reference and override notify_death()

        public virtual void notify_death(Reference reference)
        {
              // Compare reference var with the pointer you have to spaceship
              if(reference == mySpaceshipPointer)
             {
                // This means the class is being deleted so either delete this one as well or just put
               // mySpaceshipPointer to null
               mySpaceshipPointer = null;
             }
             base.notify(reference);
        }

On a list case you would remove that reference from the list.

This solution is obviously not the best but it turned out pretty great for my games. I don’t have to care how many dependence there are. I delete one and all objects will be warned. How they treat that warning is up to you.

Leave a Reply