How to create and load different levels in MonoGame?

Hi all!

I’m new to MonoGame and C#. I’ve been using MonoGame for about a month now and I’m making good progress, but there is one thing I have a hard time to figure out or find any tutorial, documentation or answer about it. And that is how MonoGame can handle different game levels and be able to switch between the levels, go from level1 to level2.

I was searching online and what most people seem to do is simply control when certain sprites and text drawn on the screen and when to update them by simply changing a variable when we are at the main menu or in the game. But it doesn’t seems the right solution when you try to make a big game with 10, 20, 30 levels Or is it?
Do I need to use the same Load, Update and Draw functions of Game1 to get the content loaded, updated and drawn for every single level in the game? But how would you load/unload content from memory as you need it for the levels? What I want to do is wipe level1 from memory and get level2 loaded then wipe level2 and get level1 loaded updated and drawn if I want to, when I want to.

I would like to do this right but I’m not sure how. I can’t find any information on this.
Is there any tutorial, documentation, example or a book that do explain this topic in detail?
It would be a 2D game.

I would appreciate any help.

Thanks.

1 Like

Sure is a broad subject, you can approach this in many ways, really depends on how you intend to structure level data, how you read it and all that jazz. I do agree if you have many levels that changing just an hardcoded variable for each level would be a bit of a mess to maintain.

What I would do is to have a LevelManager class, this manager has the function to laod and unload what you need in the level, the manager also contains a collection of Level classes, how you populate those level classes is up to you, not sure how you actually prep levels, but it shoudl just contain info on what to content to load and where.

In your Game derived class then you coudl run Update and Draw based on the current Level selected in the LevelManager, then you can select a new level from your menu or wherever you want by just setting the LevelManager current Level to something else, is up to you how to deal with the checks if the level is currently loaded or not, but the concept of it is to have a hub to manage all your levels that you can call and change thigns in it instead of putting hardcoded variables all over your Game class.

Hope I was clear, I’m not the best explainer out there.

1 Like

Thank you for the reply. The explanation was clear I’m really appreciate it. I do understand the concept of having a LevelManager to select levels and control what to load and what to unload for which level and then use the Update and Draw methods to draw and update the selected level, but I would appreciate if there would be any tutorial or book that would demonstrate something like this in practice.

I did find a library called Nez that among many other things do offer a simple solution to create and manage levels (scenes) in MonoGame. The library offer many useful features but in case someone prefer to do it on it own way, it certainly provide a nice example how something like a LevelManager could be implemented in practice :slight_smile:

I find it very useful even just to look at the source code.
Link:
https://github.com/prime31/Nez