+1 vote
by (220 points)
edited by

I have used Dan Cox's tutorial on Working with Google Sheets in Twine to be able to log gameplay data from my game.

Now, however, I need to do the reverse: instead of porting data out of Twine into Google Sheets, I need to be able to pull from Sheets into Twine. This is for a research project, and in order to map each gameplay onto the right subject/player, I need to have each log in with a specific ID.

Unfortunately, I can't just use a simple array with a pre-set list of IDs for the user to log-in and check against; I need to be able to dynamically update the list of IDs without updating the Twine game itself. If I DO use an array in Twine, I can get the conditional and log-in to work just fine; I just can't get it to work with an external array.

Thought I'd use Google Sheets, but I can't manage to get it to work. I'm not super-skilled with Javascript, which may be the problem. I've tried using sheetrock.js's "results" variable as an array to check the user-inputed IDs against, as well as using fetch from Google API. Every attempt returns the same result: the Twine IF conditional never reads the variable - it's always undefined.

I don't really have code, because absolutely nothing works. Here's the static array that works, if that helps.

<<silently>><<set
	$validIDs to [
	"roseslug", 
	"demo", 
	"beta"]
>>
<</silently>>
<<textbox "$codename" "" autofocus>>
<span id="textbox-reply"></span>
<span id="textbox-submit">\
    <<button "Log In">>
        <<set $codename to $codename.trim().toLowerCase().replace(/\s\s+/g, "")>>
        <<if $validIDs.contains($codename)>>
		<<goto [[Start]]>>
        <<else>>
            <<replace "#textbox-reply">>\
                Incorrect codename.  Please try again.\
            <</replace>>
        <</if>>
    <</button>>\
</span>

 

2 Answers

+1 vote
by (220 points)
selected by
 
Best answer

OH!!! I got it to work!!! Using sheetrock.js.

Basically:

  1. Totally rip-off the Google Spreadsheet Testing from Dan Cox's tutorial (first bit of code - loads sheetrock.js and your Google Sheet). Put the javascript portion in the Story Javascript of Twine.

  2. For "var = mySpreadsheet", put in the url of your own Google Sheet (really, straight from the address bar).

  3. Where he has "State.variables.response = response.rows[0];", replace this with "State.variables.response = response.html;". (It was this bit that I couldn't get straight!!!)

  4. Comment out the "query" line (this returns the whole sheet).

  5. In the passage, I've used this code:

<<textbox "$codename" "" autofocus>>
<span id="textbox-reply"></span>
<<button "Log In">>
        <<set $codename to $codename.trim().toLowerCase().replace(/\s\s+/g, "")>>
        <<if $response.includes($codename)>>
		<<goto [[Next]]>>
        <<else>>
            <<replace "#textbox-reply">>\
                Incorrect.  Please try again.\
            <</replace>>
        <</if>>
		<</button>>

And voila!!! A log-in page that is dynamically updatable through Google Sheets.

by (36.8k points)
Cool.  Glad you figured it out!  :-)

And then shared the answer!  :-D
0 votes
by (36.8k points)

Responded to this question in the r/twinegames subreddit here:

Importing data from Google Sheets for array in Twine (Sugarcube 2)?

by (220 points)
Didn't work. :(
Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...