Multiplayer Games using Google Sheets

Google Sheets For Multiplayer Prototyping

The most essential part of a multiplayer game is the ability for players to communicate with each other in some way. The simplest way to achieve this is for players to have shared access to a database that they both can read from and write to. When building multiplayer prototypes, it can be a hassle to setup a server and database and to then manage connections between players. What if there was a free database that existed somewhere and which players could perform operations on? Well, a database can simply be a table consisting of columns and rows; exactly the same as a spreadsheet. Google Sheets is a popular free online spreadsheet application which can be read from and written to via APIs.

The idea here is to use Google Sheets as our backend database for our multiplayer game prototypes. In order to do this, we need to be able to read and write to the sheets via Javascript. To read from the sheet, we can use a library called Sheetrock which allows us to quickly connect to a Google Sheet and query it using Google's Visualization Query Language. Writing to the sheet is a little bit trickier as it involves writing a script using the Google App Script scripting language. We can write a function to update specific rows in the sheet. This function is then invoked via an HTTP POST request and performs the update.

In order to structure things properly, each row in the sheet must be identified by a unique id. This id can then be used to retrieve and update information in the specific row. When a player sets up a multiplayer game, a new id is randomly generated and a row is added to the sheet with this id. The Google App Script returns the id that was generated and the player is presented with a URL which includes the id. The player can then send this URL to a friend to challenge him to a game. 

When the opponent browses to this URL, we can extract the game id from the query string and use it to read and write to the database. In order to detect changes to the database, the clients must poll the Google Sheet periodically. By interacting with the sheet in this way, the players can effectively play with each other. This type of setup lends itself well to turn-based multiplayer games.


Popular posts from this blog

Caribbean Games - Part 2: Pitch

Caribbean Games - Part 3: Cricket

Caribbean Games - Intro