Converting and Loading Levels from GameMaker Studio with JSON


I've made a lot of progress on porting the game to MonoGame, but I know I haven't kept up with this devlog.  So the development is actually a lot further along than the part I'm going to write about today.  So with that in mind, let's look at today's topic, which is how I got game data from GameMaker Studio over to MonoGame.

This will be a short entry, but there's some good tutorial links to check out if you need more detail (they do a lot better job of explaining the details than I would probably do).

I made the decision to export the game level data from GM Studio in a JSON format. I know that GM Studio has built-in functions (json_decode and json_encode) to deal with storing JSON data. Here's a sample of what the code I wrote looks like for the JSON encoding in my GM game:

GM Studio Code Screenshot

This is the code I used to write level data to a text file in JSON format.

Working with JSON in GM Studio might be a bit confusing at first, but I followed a really helpful tutorial written by Jason Lee Elliott, which you can find here: http://jasonleeelliott.com/2015/05/21/gamemaker-tutorials-using-json-data/

Here's the basic idea.  JSON format is kind of like a key/value format, in which objects consist of a name or key and a corresponding value.  You can think of it like a dictionary, where each entry is a word that has a matching definition.  In fact, if you've programmed in a language like Python, you might have seen actual data structures called "dictionaries" which are built on this same idea.  GameMaker's scripting language also has such a structure, but it's called a "map" instead of a "dictionary."  You might notice that there's a similarity between JSON format and a map.

As I mentioned, GM Studio has a method called json_encode, which takes a map, and creates a JSON object based on the key/value pairs in the map (read more about this function here: https://docs.yoyogames.com/source/dadiospice/002_reference/file%20handling/json_...).  So you can see that on line 5 of my code, I'm creating a map called info_map, and in the next few lines, I'm adding some level information to that map, with the name of each piece of information, and then the value (the number of target bounces, the  background image numerical index, and the number of goal stars on the level).

Then I create a list of the walls in the level.  It will be list because there are lots of them in a level.  On line 14, where it says "with(obj_wall)" I'm getting each wall and doing something with it.  For each wall object, I created a map with the position variables (x and y), then added the map to the wall list, marking it as a map. That way the json_encode method knows to include the information nested in each wall map . This will be the approach I will take for all of the other game board objects.

After adding the wall list to my level info map on line 26, I can then call json_encode on my map to create a JSON string, which I can then write to a file to be read into my MonoGame project.

Then, in the MonoGame project, I installed JSON.Net (using NuGet). I used this link to get info on installation: https://riptutorial.com/json-net/example/6086/how-to-install-json-net-in-visual-studio-projects.

To read the external text file, I used TitleContainers, as suggested at this link: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/7fcea210-8405-4a38-9459-eb0a361681cc/using-txt-file-in-xna-game?forum=wpdevelop.

So that's the basic idea of how I am exporting my game level data from GM Studio and reading it into my MonoGame project.  This code demonstrated the walls, but I have repeated the same process I used for the walls with the other objects , such as the ball, the goal stars, the moveable game pieces, and so on.

If you're not familiar with JSON, I highly suggest you look into it; it's very useful in web development (if you've worked with objects in JavaScript, JSON will likely be familiar to you) and has other applications for storing data, such as you've seen here.


Get Brain Bouncer

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.