How does the Initialize method work?

Hello to all,

I am completely new to Monogame so I apologise if the question is too simplistic or this is the wrong place.

I have just started translating my game from Pygame to Monogame and am a bit confused about the Initialize method. My understanding is that the method is only called once. I now have a method in my Game class that prevents the screen from scaling below a certain value:

    void Window_ClientSizeChanged(object sender, EventArgs e)
    {
        _graphics.PreferredBackBufferWidth = Window.ClientBounds.Width;
        _graphics.PreferredBackBufferHeight = Window.ClientBounds.Height;

        if (_graphics.PreferredBackBufferWidth < 1024)
        {
            _graphics.PreferredBackBufferWidth = 1024;
        }
        if (_graphics.PreferredBackBufferHeight < 768)
        {
            _graphics.PreferredBackBufferHeight = 768;
        }

        _graphics.ApplyChanges();
    }

The method is called in the Initialize method:

Window.ClientSizeChanged += new EventHandler(Window_ClientSizeChanged);

This also works as it should, I am just trying to understand why. Actually, in my understanding, this call should be made in the Update method, from where it also works. Is it possible that the Initialize method is also called continuously?

Initialize is called once, but the functions can be used at any time… AFIK

And Welcome to the Community!

Happy Coding!

Welcome Alexander,

What you should lookup on google is “C# Event Handling”, there are many sources explaining what is happening here.

in short:
Window has a size.
When Window.Size is changed, the Window “fires” ClientSizeChanged

in your Game you register your Window_ClientSizeChanged-Method to the ClientSizeChanged from the Window. It is like adding the Method to a list of things the Window has to call, when it’s size changes.

1 Like

Awesome, thank you very much! That’s why there is a “+=” to give “Window.ClientSizeChanged” a new EventHandler! Again, thank you guys! That really helped

1 Like