Loading Tiles from a Json

I’m working on a platformer right now, which is the first game I’ve made with MonoGame (I’ve used it a lot, I just haven’t finished a game before), I want a way to load levels using tiles. I figured out how to create a system for tilesets, tilemaps, collision, etc., but I would like to build the levels in an engine, rather than instantiating every tile in my code. I’ve checked out MonoGame.Extended.Tiled, but I find it a little convoluted, and I’d also like to build everything without any packages. The program Tiled has a way to export as a JSON file, which from what I know, is a JavaScript format, but it can also be parsed in CSharp. I’m having trouble finding any solid tutorials online. Does anyone know a place they could direct me for approaching this? Ideally I could just load information from the JSON into a 2d array of some sort. Thanks beforehand.

I know you say you don’t want to use any packages, but I’d highly recommend re-evaluating that as a lot of otherwise mundane stuff is done for you. In this particular case, JSON parsing. I get not wanting to use MonoGame.Extended for its Tiled support (I’ve had issues with this myself), but writing your own Json parser to boot is a lot to take on. I would look into this…


(It’s also on NuGet)

Also, it looks like the Tiled file format itself is just an XML file, so you could use the built-in .NET library to parse that.

Anyway, past that, you can see the wikipedia article (or other places you find on google) for the file format.

Good luck! :slight_smile:

2 Likes

Thanks. I actually didn’t realize Microsoft didn’t have built in JSON support. I’ll check it out.

Newtonsoft can process the JSON file but you will need to create the type to deserialize to. You’ll have to figure out the layout of the file and provide the appropriate attributes. Tiled is pretty well documented so it shouldn’t be too big of a problem. If not you should be able to figure it out from the XML/JSON.

1 Like

There is also System.Text.Json as alternative.

2 Likes