Inventory Management w/ JSON

A game based around items take a lot of forethought into how you will manage your database and your save files. With GameMaker I decided to use JSON for several reasons.

  • Readability on the backend
  • GameMaker automatically sorts JSON data into Data Structures of Maps and Lists, and Lists OF Maps, and as far down the rabbit hole as you want to go.
  •  With an addon, Google Sheets can export as JSON – and it is beautiful.


First I had to figure out the workflow for accessing my data. That part was easy to sketch up. A simple JSON database which contains all the item information for displaying said item information, and then a second JSON database structure to save all the valuable game data to. This is a million times better than using arrays with GameMaker as I am not iterating through every-single-index every time I want to do something, I can simply access it directly! Woo!

Now let’s take a look at the Google Sheets export with a small test database:

Isn’t that just super pretty? And yes, it does support nested JSON objects and arrays!

By the way, this addon is called: Export Sheet Data and can be found by going to Addons > Get Addons inside Google Sheets. 

Specifically for the database information, I saw no reason to keep it in a Map when I would always need to find each index which is an additional data structure in memory, so instead I have chosen to load the map into an array using Enums for easily accessed readability.

That’s all it takes to load my database of items and I now have access to all my item information through db.itemData[ID, ITEM.VALUE] – very readable when rendering in the draw events of my inventory! Even more readable when parsed through a function called item_get_info(id, key); This isn’t the most ‘scalable’ method as if I need to update the database structure I have to also update the main import file and ENUM listing. However it does reduce a data structure loaded into memory, reduces map calls, and gives great readability. Pros and cons for everything!

The bag save JSON is very simple, each object represents a slot (1-21), with the ID of the item behind held and the amount being held.

Since I want to access the save data map directly I have no need for arrays so we’ll be keeping the DS_MAP in memory, I simply load the JSON data directly into a Map called bagData and access that map as I update files removing the array setup completely. Saving the file system is as simple as this:

Pretty sweet, right? Just remember GameMaker does 2 things:

  • Keeps maps in an unordered list, so you’ll never know what order it will export your JSON structure.
  • Spits out JSON in a single line instead of a nice readable format.
  • GameMaker loves to append a ton of zeros to your numbers which doesn’t affect anything on import but does add another readability issue. Just a price we have to pay for using a system that automates all the real/int/number management.
  • Example:
    { “14”: { “AMOUNT”: 0.000000, “ID”: 0.000000 }, “10”: { “AMOUNT”: 0.000000, “ID”: 0.000000 }, “16”: { “AMOUNT”: 0.000000, “ID”: 0.000000 }, “18”: { “AMOUNT”: 0.000000, “ID”: 0.000000 },

Once we have the database loaded it’s just a matter of displaying the information and editing the Maps as needed. Allowing simple management of items including manual and automatic sorting! Woo!

Bonus:
I also did some basic work on transitions this week for entering/leaving buildings.


(Art is WIP and not a final representation of the game)

Got any comments or questions about what I am doing? You can find me on Twitter!