CurrentDisplayMode is sometimes null when starting

A user gets an “Object not set to a reference…” crash on this line:

ResolutionWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;

I have not reproduced it myself, but this code runs during startup, in the game’s constructor. So this is before Initialize():

What could be the reason? Initialize hasn’t completed or…?

That can happen when MonoGame isn’t dpi aware and users have dpi scaling enabled. You can add a manifest to let your application be dpi aware.

If you’re unsure how, check out the PR’s on GitHub, I have one up that adds a manifest to the templates. A pr was also merged to throw a better exception when this occurs

1 Like

So I can repro the crash on my own?

Probably if you enable dpi scaling

So the user should be able to run the game if he disables scaling…

This happens because MG gets the wrong resolution when dpi scaling is enabled and it thinks the GraphicsDevice doesn’t support that resolution.

Yes. He might need to log out and back in for changes to take effect

OK. But I would like to keep scaling as an option. Some users prefer it. So I need a fix so the game can run with and without scaling.

Then add a manifest like I said

But doesn’t the manifest just prevent Windows from scaling the game?

No, it makes the application dpi aware

A DPI aware application must do the following:

“Change window dimensions to maintain a physical size that appears consistent on any display
Re-layout and re-render graphics for the new window size
Select fonts that are scaled appropriately for the DPI level
Select and load bitmap assets that are tailored for the DPI level”

None of that is handled by MonoGame as far as I know. It all has to be coded in the game.

That’s true. If you want to scale things in your game with dpi scaling you have to handle it yourself.

As I understand it, the manifest tells Windows that the game is DPI aware, even if it doesn’t actually scale itself. Then Windows won’t scale the window.

But,many users want the game to be scale up. So adding the manifest would mean that they have to override the manifest and enable scaling. I guess I can live with that.
But that does not help if the game crashes because of the scaling?

If you want to scale your game when the user has dpi scaling enabled that really, really, really complicates things so I recommend against it. The thing is your app can’t figure out the right resolution when it’s not dpi aware and dpi scaling is happening. It will get scaled resolutions, which the GraphicsDevice does not support which causes the Exception your users are getting. If you make the app dpi aware it can get the actual resolution. That doesn’t mean it actually handles dpi scaling nicely. So if you make your app dpi aware it will stop crashing on startup when dpi scaling is on and nothing more. If you want to scale with dpi scaling then you need to implement that in your game yourself.

Thanks, this is what I want. I will test the manifest solution.

1 Like

I used Process Explorer to inspect the running program for DPI awareness. It always shows “Per Monitor aware”, with or without the manifest.

This is the program I use:

You can test this, right? I ran into this issue and adding the manifest or making the app dpi aware in code solved it.

I have a high DPI laptop that I will try to test it on.

If you run Windows 10 you can change scaling (under Settings->Monitor I think)