Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/johnaste/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/johnaste/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/johnaste/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Twine Game Data to Google Sheets via Javascript version 2


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/johnaste/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/johnaste/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/johnaste/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Using Twine, a free, open-source, text-based game software, you can build choose your own adventure games that explore the untaken paths in literaturepromote empathy through simulated experiences, and provide role-playing adventures in historical scenarios. Twine games are often used in the classroom, because you can quickly build an educational experience about whatever subject you choose. They are also heavily used in the interactive fiction world as a medium for short stories and novels.

I wrote the first version of this post back in October, while Keegan and I were the XP Twine workshop. In this post, I explained how to capture game data from Twine and push it into a Google Sheet. However, in discussing how to implement the original post with @hpkomic, I found out that the javascript code that I wrote back then was invalid because of something called CORS. There are many protections on the web to prevent people trying to inject malicious material into sites. CORS protections kick in when someone is trying to make changes on one domain from another domain, or “cross-origin.” My old code was not CORS compliant, so it didn’t work properly.

The first section of the code as the same as it was back in October. It’s the second section, “Twine Game Code,” that’s new.

Twine games take the form of HTML files with embedded CSS and JS. In my latest round of tinkering, I figured out how to use javascript within a Twine game to send an HTTP post message to pass game-play data to a Google Spreadsheet, thereby creating a database that records each game-play.

Google Sheet/Apps Script Code

In order to track this game data, I suggested that we push the data from Twine to a Google Spreadsheet. Following the lead of Tom Woodward, I’ve found that Google Spreadsheets are a relatively easy place to collect and analyze data. I wanted to use Google Scripts, which are mostly javascript and a few custom functions, to receive data and parse it into the cells of the Google Sheet.

Martin Hawksey wrote a blog post a few years ago called “Google Sheets as a Database – INSERT with Apps Script using POST/GET methods (with ajax example).” Martin had set up an Ajax form that could be embedded in any website that would pass data to his Google Script which would then record it in his Google Sheet. Martin’s code (below) receives an HTTP Get or Post call generated by an Ajax form, parses the parameters of that HTTP call, and stores those parameters in a Google Sheet. Martin also provides comments in his code to help users customize the Google script and initiate it as a Web App.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//  1. Enter sheet name where data is to be written below
        var SHEET_NAME = "DATA";
 
//  2. Run > setup
//
//  3. Publish > Deploy as web app 
//    - enter Project Version name and click 'Save New Version' 
//    - set security level and enable service (most likely execute as 'me' and access 'anyone, even anonymously) 
//
//  4. Copy the 'Current web app URL' and post this in your form/script action 
//
//  5. Insert column names on your destination sheet matching the parameter names of the data you are passing in (exactly matching case)
 
var SCRIPT_PROP = PropertiesService.getScriptProperties(); // new property service
 
// If you don't want to expose either GET or POST methods you can comment out the appropriate function
function doGet(e){
  return handleResponse(e);
}
 
function doPost(e){
  return handleResponse(e);
}
 
function handleResponse(e) {
  // shortly after my original solution Google announced the LockService[1]
  // this prevents concurrent access overwritting data
  // [1] http://googleappsdeveloper.blogspot.co.uk/2011/10/concurrency-and-google-apps-script.html
  // we want a public lock, one that locks for all invocations
  var lock = LockService.getPublicLock();
  lock.waitLock(30000);  // wait 30 seconds before conceding defeat.
 
  try {
    // next set where we write the data - you could write to multiple/alternate destinations
    var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
    var sheet = doc.getSheetByName(SHEET_NAME);
 
    // we'll assume header is in row 1 but you can override with header_row in GET/POST data
    //var headRow = e.parameter.header_row || 1; Hawksey's code parsed parameter data
    var postData = e.postData.contents; //my code uses postData instead
    var data = JSON.parse(postData); //parse the postData from JSON
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
    var nextRow = sheet.getLastRow()+1; // get next row
    var row = []; 
    // loop through the header columns
    for (i in headers){
      if (headers[i] == "Timestamp"){ // special case if you include a 'Timestamp' column
        row.push(new Date());
      } else { // else use header name to get data
        row.push(data[headers[i]]);
      }
    }
    // more efficient to set values as [][] array than individually
    sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
    // return json success results
    return ContentService
          .createTextOutput(JSON.stringify({"result":"success", "row": nextRow}))
          .setMimeType(ContentService.MimeType.JSON);
  } catch(e){
    // if error return this
    return ContentService
          .createTextOutput(JSON.stringify({"result":"error", "error": e}))
          .setMimeType(ContentService.MimeType.JSON);
  } finally { //release lock
    lock.releaseLock();
  }
}
 
function setup() {
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    SCRIPT_PROP.setProperty("key", doc.getId());
}

I edited Martin’s original code in lines 39-41. In his code, he’s looking for Post data in a slightly different format than what I generate. Rather than using the parameters from the HTTP Post, my code uses the data from the Post.

Twine Game Code (newer version)

Unlike the earlier version, this code now uses Ajax and jquery, to pass variables that had been collected during gameplay in a Twine game. Twine is built on javascript, so I decided to replace Martin’s Ajax form with a javascript HTTP Post function embedded in Twine. Based on research on how Twine works, I decided that the best way to do this would be to write the javascript code directly into a Twine game passage. My passage, called PostData, would presumably come at or very near the end of my game after all interesting variables have been set:

Screen shot of a Twine game passage showing the code explained in the surrounding post text.

Dan Cox provided me with sample jquery code and explained it. We then wrap the code in Twine’s script syntax <script></script>. This code sends an HTTP Post to whatever url is provided (like the url for your Google Script Web App) with a json package defined in the var data line.

First you need to reference the jquery library in the head of your file:

<script src=”jquery-3.3.1.min.js”></script>

Once that’s been loaded, you place this code at the end of your game (please note that you need to add the <script></script> modifiers in Twine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var sendData = JSON.stringify({
"var1": harlowe.State.variables['var1'], 
"var2": harlowe.State.variables['var2'], 
"var3": harlowe.State.variables['var3'], 
"var4": harlowe.State.variables['var4'], 
"var5": harlowe.State.variables['var5']
});
 
$.ajax({
url:"Your Url",
method:"POST",
dataType: "json",
data: sendData
}).done(function() {});

However, in order to pull variables out of the Harlowe version of Twine that I was using, I also needed to add the following code by editing the Story Javascript:

Twine Javascript Screen Shot

This bit of Javascript passes all variables defined within the Twine game into an array (window.harlowe) that is accessible by Javascript code that is embedded in the game. Here’s the code in case you want to try this out:

1
2
3
if (!window.harlowe){
	window.harlowe = {"State": State};
}

I hope this work will be useful in studying any Twine game to see how players are moving through the game. You could record any variables in the game and also the games ‘history’ to see which passages each player went through. This has obvious uses for educational games in being able to provide feedback to players, but it also has implications for game design more broadly with the increased use metrics.

Implement in your own game

In order to implement this for your own game, I would suggest following these steps:

  1. Copy the Javascript code above (starts with if (!window)) into your Twine game’s Javascript panel
  2. Copy the PostData code above and paste it into a TwinePost passage towards the end of your game
  3. Then replace the variables in the TwinePost passage so that harlowe.State.variables[‘var1’] becomes harlowe.State.variables[‘your variable name here’] for each of the variables you want to track
  4. Click this link to get a copy of my Google Spreadsheet
  5. Make sure the column headers in the spreadsheet match your variable names from the TwinePost passage
  6. Share your Google Sheet, so that anyone with the link can edit it. This will allow the incoming data to post
  7. In the Google Sheet, click on Tools->Script Editor and follow Martin Hawksey’s instructions for steps 2-5
  8. When you publish your Script as a Web App, it will give you a URL for the Web App. Copy this URL and paste it into the URL variable in your TwinePost passage code.
  9. You’re done. Play your game and see if everything works. If it doesn’t work, tweet at Tom Woodward. He’s good at fixing code and has nothing but free time on his hands.

I am excited about this code because it answers a question for several of our faculty members and makes Twine games more useful as formative assessments. Hawksey did an excellent job in keeping his code very generalized, and I’ve tried to preserve that, so that you can track whatever variables you want.

You could also use the HTTP Post javascript code outside of Twine in any other web site or web app to pass information to your Google Sheet. Tom has blogged a couple of times about using code to send data to Google Forms and autosubmitting into a Google Spreadsheet. I think the process described above denecessitates that Google Form pass through and moves us a step closer to Google sheets as a no-SQL data base alternative.

13 comments

  1. Megan

    This is so useful! Thank you for working this out & writing it up; I’ll definitely be using it in all my twine games to track player data.

  2. Rebecca G

    Hello, I’ve tried this a few times now and am getting the following error; if someone is able to help me figure out what is going on I’d be grateful, thank you!

    an embedded page at v6p9d9t.ssl.hwcdn.net says

    Sorry to interrupt, but this page’s code has got itself in a mess.
    SyntaxError: Unexpected token <

    at n
    at Function.globalEval
    at text script
    at Q
    at r
    at XMLHttpRequest.
    at XMLHttpRequest.send
    at Object.send
    at Function.ajax
    at Function.he._evalUrl

    (This is probobly due to a bug in the Harlowe game engine.)

    1. John Stewart

      Are you using the < symbol anywhere in the js section or in any of your variables? It could be that there's an open tag somewhere or extra "<" that's making the system think there's an open tag. That's my only guess at first glance.

      1. Rebecca G

        Thanks John for your quick reply!

        I’ve scoured the thing but don’t think that it’s a case of a stray character; maybe there is some problem coming from a related workaround I’m implementing from another tutorial (https://exgeekfacto.blogspot.com/2018/02/InputStyles.html) that lets me a use better data entry form than just the (prompt:) macro. This other tutorial uses

        window.Harlowe = { ‘State’ : State };

        which I replaced for your related if not / lowercase version

        if (!window.harlowe){
        window.harlowe = {“State”: State};
        }

        …but it still seems broken, so maybe there’s a nuance that I don’t know about given my noobish javascript skills ;).

        I am working on a game design brainstorming tool that hopes to collect user entries into a spreadsheet so we can see how the idea develops over time and between multiple participants. It’s still a work in progress, but if you are interested in considering the bug in action at
        https://mechabecca.itch.io/reflectwip (Password: “Reflect”) I’d certainly appreciate your eye!

  3. Eric

    This is all good and great to know, but useless if you can’t pull data from the spreadsheet and place it back in twine.

    1. John Stewart

      Hi Eric,

      I think Dan addresses how to pull data into Twine games in some of his work. My project was more about trying to use Twine to collect information. I’d love to see the combination of the two. What are you wanting to do with the two way communication?

      1. michele

        I’m interested too in this topic. What I’m thinking is a way to update what a user reads on his smartphone, simply making his phone pulling every some seconds a variable value from the GSheet. This way i could somehow write a big, single twine page that says:
        If “status” = 1
        show this text
        else if status = 2
        show this other text

        and so on.
        Any idea how to achieve that?
        Thanks for you jb anyway, i implemented till now and works great.

        1. Amy

          I’m also interested in the possibilities of pulling data back from a google sheet into Twine (as a way of allowing the current player to see previous responses by other players to a question). I know the code has been written for Sugarcube, but was wondering if anyone had been able to work out how to get it working in Harlowe?

      2. Gilbert Walker

        If you could get 2 way communication to work, wouldn’t this mean you could make a turn-based multiplayer twine? Think of that! A co-op twine game. How AMAZING would that be?!?!

        If anyone knows how to get that working I’d be SOOOO keen to learn! 🙂

  4. JT

    Hi Dr. Stewart, thanks for posting this helpful blog!! Is there any way for me to track how long players spend in each passage using your Google Sheets? I am totally new to coding, so I apologize if there are some concepts I do not understand. Thank you in advance!

    1. John Stewart

      I’m sorry I didn’t see this in anything like a timely manner. In the same way that you log the choices that users make, you could add a timestamp as to when those submissions come in. I’m not sure this would be the most efficient way though, because you’d basically be receiving posts to your Google Sheet for every click that someone makes, and then keeping a log of all of those submissions. There are some timers built into Twine that you might be able to use there are probably some other custom Javascript ideas that would work a bit better on the client side for gathering this data.

  5. Mimi

    For people who’re trying to figure out how to implement this for Sugarcube instead of Harlowe, just change the

    “var1”: harlowe.State.variables[‘var1’],
    “var2”: harlowe.State.variables[‘var2’],
    etc

    to

    “var1”: SugarCube.State.variables[‘var1’],
    “var2”: SugarCube.State.variables[‘var2’],

    also, you don’t need to add the (window.harlowe) code to your javascript file. Everything else is the same! Hope this was helpful!

Leave a Reply

Your email address will not be published. Required fields are marked *

css.php