Search code examples
c#listinheritancemonogame

Adding objects to a list from within a subclass (C#)


I'm not understanding how to add objects to a list when I'm putting the code in a class definition. I'm working with C#.

I'm making a platform game and have set things up so that every object in the game, whether a player character, an enemy, a moving platform or a stationary object, all inherit the same properties held in my "StaticObject" class (which itself inherits a "Sprite" class), building on top of this depending on their specific function. I want to add all objects, regardless of their type, to a list to help me check for collisions.

For example, one sequence of inheritance is: Sprite > StaticObject > MovingObject > Character > PlayerCharacter

I created a "GameInfo" class (and made a static instance so that its properties can be accessed from anywhere in the game) and defined my "gameObjects" list in there:

class GameInfo
    {
        public static GameInfo gameInfo { get; private set; }
        public List<StaticObject> gameObjects { get; set; }

        public GameInfo()
        {
            GameInfo.gameInfo = this;
            gameObjects = new List<StaticObject>();
        }
    }

Then in the constructor of my "StaticObject" class (which all game objects inherit) I tried to add the object being constructed to the list in "GameInfo":

class StaticObject : Sprite
    {
        public StaticObject(Texture2D texture, Vector2 position, SpriteBatch spriteBatch) : base(texture, position, spriteBatch)
        {
            GameInfo.gameInfo.gameObjects.Add(this);
        }

        public override void Update(GameTime gameTime)
        {
            // Collision code.

            base.Update(gameTime);
        }
    }

This doesn't work and I get the error:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

I can't work out how exactly to add every object to my "gameObjects" list at the moment they are created, without having to explicitly add each one when the main game code creates an object. I want the addition to happen automatically.

I've seen a similar question asked elsewhere but I couldn't get my head around the answer. Any help on this would be greatly appreciated! Thanks! :)


Solution

  • Instead of GameInfo.gameInfo = this; in the CTOR do public static GameInfo gameInfo { get; private set; } = new GameInfo();

    Also, you should make the Constructor private.

    While this will do for the moment, it will be beneficial for you to dig yourself deeper into the Singleton-Pattern if you are planning on doing more coding. And of course Design Patterns in general are always worth a read.

    As to why it didn't work:

    You (tried to) set the static reference to the (singleton-)instance of the class in the constructor. But the constructor has not yet been executed when you try to access that static field. It never would be executed with that code unless you did so explicitly (which you could since the CTOR is public).