Couple of questions from a new user (About Game class)

I’m coming from Unity, wanted to drop it for something that let me some more freedom on how game work instead of having to rewrite whole systems and hack around the existing ones, so I thought was better to start from scratch then.

I’m getting around how MonoGame is structured and is pleasant to use. At the moment I have a doubt that I would like an input from someone experienced with the framework. Noticed that classes like GraphicsDeviceManager and GraphicsDevice are dependant from the base Game class, so referencing the two from outside it can be a bit problematic at times.

First try was to pass the Game class in the constructors of the classes that should use it, but it quickly become spaghetti code as not all components need that kind of info, so I thought to make a static class having both those other two in it for a global reference, but I’m unsure if is a good idea, this is what I do:

In the Game based class on Initialize I populate a static class called Display, with those simple lines:

            Display.SetDevice(GraphicsDevice);
            Display.SetScreen(graphics);

Then this is my static Display class:

public static class Display
{
    static public GraphicsDevice Device { get; private set; }
    static public GraphicsDeviceManager Screen { get; private set; }

    static public void SetDevice (GraphicsDevice device)
    {
        if (Device == null)
            Device = device;
    }

    static public void SetScreen (GraphicsDeviceManager screen)
    {
        if (Screen == null)
            Screen = screen;
    }
}

So can just cal lDisplay.Screen or Device from other classes and have it working, but would this be a good method in the long run, or what could be the alternatives?

Thanks.

Hi Neurological, welcome! This looks fine to me, and I don’t think you will run into many issues with it. The GraphicsDeviceManager contains a reference to the GraphicsDevice, so one suggestion I have is to condense it by having only the SetScreen method and making the Device property point to Screen.GraphicsDevice. My one concern is it’s inaccurately named; why did you choose to name it Screen?

In my projects, I usually like having a Singleton that holds this information. I find Singletons easier to work with and clean up than static classes, but that’s just my preference since I also put more in them.

Above all, you have the freedom to structure your game/engine how you want. If this approach works for you and your needs, then it should be perfectly fine.

1 Like

Hi, thanks for the reply. Thought of making it a static class to ahve less verbose code, I do have a main singleton that holds the important stuff, but since the device data are needed in a lot of places for some calculation to write the whole singleton class plus the device would become long, is just in aincovenience since my monitor is of a crap resolution and have to deal with sapce a lot to read code.

So GraphicsDevice is part of GraphicsDeviceManager? I thought were two separate things, sure I can simplify then isntead of two different calls.

As for naming it Screen is just a name convention I was used in unity, since all graphics device functions are in a class caleld Screen, but I do agree that is bad naming, have to slowly get used to structure stuff differently I guess.

But anyway thank you again for your tips, sure helps a lot.