0 votes
by (230 points)
Hello all!

I am considering using Twine as one of my materials for my research. I plan to make a Visual Novel-style game where the player will encounter a number of decision points. I've read enough posts in the Wiki and Q&A about the basics but I wanted to see if anyone knows how to save the data into google sheets?

The data that I plan to save are: the player name, the game history (the decisions the player made), the time it took them to make each decision. I came across this post (http://www.johnastewart.org/coding/twine-game-data-to-google-sheets-via-javascript-version-2/) that does what I intend the game to do but it's only limited to the variables (w/c I think is just the name and the time). Is there a way to tweak this method so it can record all of the required the data (name, history, and time) into one row per player?

TIA!

1 Answer

+2 votes
by (159k points)
selected by
 
Best answer

The following sendData related section of the linked example...

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']
});

... is creating an generic object with a set of key / value pairs, which it then converts a JSON String.

eg. key "var1" in the object equals the current value return by the harlowe.State.variables['var1'] expression.

One reason the example is limited to the values of game's story variables is because Harlowe has been deliberately designed to restrict access to it's engines internal functionality & state via JavaScript, and this is why the linked example includes a Scope Escalation hack to allow you to gain access the engine's State.variables object.

The simplest way for you to gain access to the other data you want to pass to the Google spreadsheet is to store that data in a story variable just before you execute the JavaScript that'll send it to the server.

ex. If you want to track which Passages (excluding the current one) that the Player has visited (in the order that they were visited) while playing your game then you could assign the Array returned by the (history:) macro to a story variable just before executing the JavaScript functionality.

(set: $history to (history:))

... and modify the JavaScript that creates the sendData JSON String to include the new story variable like so...

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'],
	"history" : harlowe.State.variables['history']
});

edit:
You can use methods on JavaScript Date instance to access the current time on the Player's machine, which you can store in the sendData JSON String like so.

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'],
	"history" : harlowe.State.variables['history'],
	"now"     : Date.now()
});

 

by (230 points)
Thank you! You've been very helpful! I'll give this a run in the coming weeks when I begin testing John Stewart's work around.
by (230 points)
edited by

hello @greyelf,
 

I just tried the work around on a test story and it gave an error when I uploaded the test game on philome.la. I followed the steps as well as your suggestions and I'm not sure what to make of the errors.
 

Here's a link of my published .html file if it would help.
 

Many thanks.

...