Recently I've started working with a small team on a 5-month game jam. We're making a 2D rocket-building game for Mini Micro. This involves some data tables defining the tech tree, and the rocket parts unlocked by each tech node. To make it easy for everyone on the team to view and update that data, we're keeping it (for now) online in Google Sheets — and our Mini Micro code can read that data directly. That means that as soon as you update the data in the sheet, you can just re-run the game and it immediately uses the new data.
This is an amazingly useful trick. I'll show you how you can do it, too!
1. Publish the sheet as TSV
The first step is to open the Google Sheets document you want to access, and from the File menu (within the web page), choose Share ▶︎ Publish to web.
This will pop up a dialog that lets you set various options; the most important one is the format pop-up, which you should change from "Web page" to "Tab-separated values (.tsv)".
Check over the other options too in case there's anything you want to change. Then click the Publish button.
Once you publish a spreadsheet, it's published forever and updated immediately whenever the data changes (you never need to re-publish it). That's why in my screen shot above, the dialog points out that this document is already published — I did that yesterday.
Once you publish, copy the URL that it shows you. (You can always go back and use the Share ▶︎ Publish to web command again to see that URL again if you need it.)
2. Read and parse the TSV in Mini Micro
Because we published in Tab-Separated Value (or "TSV") format, we'll get the data as a big string, with rows separated by line breaks (or char(10) in MiniScript), and columns separated by tabs (char(9)). It wouldn't be hard to write some code to split the string into a list of lines, and then split each line into a list of fields... but why bother? Mini Micro has a built-in TSV module that does all that for you!
Once you import "tsv" and get a URL to a published TSV sheet, pulling that data down from the web and parsing it (into a set of maps — assuming the first row is headers and other rows are data) is as simple as:
data = tsv.parse(http.get(url))
It's so easy, you can do it right on the command line!
If your spreadsheet has multiple sheets, the base URL will get you the default one (always the leftmost sheet, I believe). However you can access any sheet you want by going to it in Google Sheets, and noticing the "gid=" part of the URL.
See that "gid=1681045610" part at the end of the URL? That number uniquely identifies which sheet you're looking at. You can append this string (including "&gid=") to your base URL, to get a complete URL that will fetch that specific sheet.
My game data currently has two sheets: one for parts, and one for the tech tree. So my Mini Micro code actually looks like this:
import "tsv"
// Use the URL from the "Publish to Web" feature:
baseURL = "https://docs.google.com/spreadsheets/d/e/2PACX-1vRA2AQx4X9PZyyoU5mMV18MdB-OI50dx-AdShkBqMKqSaa8dFhb3USE5vtUF1JlPBjkTZFouuyF3Quj/pub?output=tsv"
partsListURL = baseURL + "&gid=22599298"
techTreeURL = baseURL + "&gid=1681045610"
// Fetch the data
partsData = tsv.parse(http.get(partsListURL))
print "Read " + partsData.len + " parts, such as: "
pprint partsData[0]
techData = tsv.parse(http.get(techTreeURL))
print "Read " + techData.len + " tech nodes, such as: "
pprint techData[0]
When I run that, I get this:
...and yes, I just gave you the real URL to my real game data. You could run this in your own copy of Mini Micro and see all the parts and tech nodes.
Go on, do it. I dare you.
3. ...There's no Step 3
And that's it! It's such a simple technique, but such a phenomenally useful thing to do. It means that the whole team can iterate quickly on the data, without having to change any files. You could even have multiple people editing it at once, all in real time, without worrying about git conflicts or other pain. It's just... frictionless.
When it comes time to actually publish your game, you'll probably want to download that data as a file (for each sheet), and bundle it with your game. Then you'll just replace http.get with file.load; the rest of your program stays the same. This will reduce your load time and ensure your game still works even without the internet (assuming it doesn't need internet access for some other reason).
But during development, this is about the most powerful use of http.get I can imagine. Try it! You won't be disappointed.




Top comments (0)