0 votes
by (120 points)

hey folks,

I'm trying to use count.io to track how many people do specific actions in a game I'm working on. Ideally, at the end of the game, I'll be able to provide the player with a telltale-like list of how their decisions compare to the average distribution of decisions made by the playerbase.

I've written extensive JavaScript modifications to Twine 1.4.X games before, but I've never previously used APIs. This one is pretty easy, however, and I was able to successfully call it using Postman, but when I tried to write it up in a Javascript mod for Twine I started hitting some roadblocks.

Here's what I'm working with right now. This is a script in my game. I've replaced my token and the names of my counts with placeholder text to make it more readable:

try {
	version.extensions['killbreen'] = {
		major:1, minor:0, revision:0
};
	macros['killbreen'] = {
	handler: function(place, macroName, params, parser) {

		var xhr = new XMLHttpRequest();
		xhr.open("POST", "https://api.count.io/1/count/[MY COUNT CATEGORY]/[COUNT I'M INCREMENTING]+", false);
		xhr.setRequestHeader("Authorization", "[MY TOKEN GOES HERE]");
		xhr.send();

		new Wikifier(place, "You have killed it.");
}
	};
} catch(e) {
	throwError(place,"serveevent Setup Error: "+e.message);
}

Here's the error I'm getting. I also replaced the count/count group names here:

Error executing macro killbreen: NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'https://api.count.io/1/count/[MY COUNT CATEGORY]/[THING I'M COUNTING]+'.

I bet this is a terrible javascript error I'm making because I really know very little about JavaScript. Any help would be appreciated, though!

1 Answer

0 votes
by (156k points)

NOTES
a. Which story format (name and full version number) are you using, as answers can be different for each one.
b, I am assuming the [MY COUNT CATEGORY] and [THING I'M COUNTING] parts of the URL you are using were replaced with actual values relevant to your project.
c. I am also assuming that the [MY TOKEN GOES HERE] value of your Authorization header actually contained your authorisation token.

The "Failed to load" error message generally means that your URL was incorrect, I suggest you check your URL to make sure that it is correct and that you have supplied a valid category and thing you're counting.

by (120 points)
yes, those things [IN BRACKETS LIKE THIS] are the things I mentioned I cut out for the purpose of posting this question, to make it more readable.

I am using a URL that worked in Postman. I am successfully able to increment various counts and delete/reset the accounts in Postman.

My story format is Sugarcane. It is the version that installs with Twine 1.4.2 in the version currently downloadable from twinery.org. I am using a Mac and am unfamiliar with where the story formats are stored on Mac and I am not sure how to find out the version of Sugarcane that currently comes with 1.4.2.
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.
...