Services contain GraphicsDeviceManager by default

Hi,

I wanted to know how MonoGame can detect whether no GraphicsDeviceManager has been instantiated, I figoured that out by looking at the source, it’s adding it to the Services. This means the Services always contain a reference to the GraphicsDeviceManager.

Now my question, can I safely use this:
var deviceManager = (GraphicsDeviceManager)Services.GetService<IGraphicsDeviceService>();

to change display settings while MonoGame is already running?
I am asking because I wanted to use that in my yet to implement GUI Options later to change display settings in the game. I think I will implement GUI with a normal DrawableGameComponent… and since that one got reference to Game I can use Services and these contain GraphicsDeviceManager… Instead I would have to pass the GraphicsDeviceManager from my class that inherits from Game to GUI somehow which is crappy.

Thanks in advance.

Yes, this is safe to do. Just make sure you create the GraphicsDeviceManager before getting the service.

1 Like

Yeah, the GraphicsDeviceManager needs to be created in the constructor of my Game class, thats so early, I only need to get the Service while the whole engine is already running.

Great to know, thank you :slight_smile:

1 Like