Best way to load rooms/levels in monogame for different types of games?

Hey I’m just wondering the most efficient way to load and save levels/rooms in a 2d monogame game. I was thinking of making a function to read txt files, and based on the values in the text files update an array with whatever values it says in the txt file, but that seems like it would take a decent amount of time to write, and before I made the commitment to write that, I wanted to know if there’s a better way. Really my question is, is this an efficient method of loading numerous levels in a game? And should I do this differently based on the type of game I’m making (like roguelike with many rooms vs just a platformer with one level on screen at a time)?

I am using Serialized objects. I have a LevelObject that defines everything that is needed for the level, then I use Newtonsoft JSON Serializer/Deserilizer to load in the level object. I then use that to create the actual game objects needed.

For my game, it’s very fast. I don’t have any actual profilier numbers, but it’s fast enough that I don’t need to profile it.

The level object has a lot of meta data that tells my game how it should be created (location, image, animation information, etc…) Then I have a real game object (which is generic) that I apply all that information to.

1 Like

I have loaded hard coded levels, and loaded from txt, and am now using XML…

XML is fast enough to load stuff faster than I can run through it.

Basically creates a catalog of all your objects or whatever, ie each item in the catalog represents an object in your game… Then for each item in the XML catalog, you can store all the variables you want. Hitpoints, position, state, etc.

XML is better than text because you can look up items in the xml file without parsing text, or knowing where the item is stored…

So your code can be like “look for character Brenda in xml file, and return to me the value for her variable hitpoints”

XML is standardized, and super effecient for how relatively simple it is, and it’s widely documented even outside games.

Also, XML can be read as a txt file, so you can read it, search it, alter it in any editor, etc…

1 Like

Loading many levels, you might want to look into multi-threading for loading the next level while you play the current one. It can literally be done in a few lines of extra code, and I recently had a succesfully resolved post about just that, you should search it.
-Like once you have code that can load a level, you just instruct the computer to run that code separately and it just does that, no questions… Code is like “When enter room_1, on other thread, load room_2”

Or you can just have a slight load between levels. I have that in my older games, and I think it’s not a draw-back…

As for loading each type game, for I would say just do XML… Anything I did before XML was a stepping stone getting there.

-I mean for some games I have used images to store level-data (this way any paint program becomes a level editor )…

But XML is just a great thing to be aware of…

1 Like

Thanks for all the help! I think I’ll have to google to learn how to use XML serialization, but I’m glad theres a better way than what I intended.

My preference is JSON as it’s easier for a human to read and more compact than XML.