0 votes
by (710 points)

For the life of me, I cannot figure out what I did wrong.

I got all the necessary files.

I build the Twine Game correctly with nw.js.

I put it on Steam (with the build), but the test achievement I’m using refuse to unlock.

 

This code I had used with StoryInit did not work. 

var greenworks = require('./greenworks');

if (greenworks.initAPI()) {
  console.log('Steam API has been initalized.');
  greenworks.activateAchievement("start");
} else {
  console.log('Error on initializing Steam API');
}

 

I put it on the Start passage instead. Did not work either.

I had looked at the game HTML file in Chrome to see the console log. No message shows up, like the code  suggest, so obviously I did something wrong, but what?

1 Answer

+1 vote
by (159k points)

You need to state the name (and full version number if SugarCube) of the story format you are using, as answers can vary based on this information. I will assume that you are using one of of the Twine 1.x built-in vanilla story formats like Sugarcane.

warning: If you are using one of the vanilla story formats then I strongly suggest you change to SugarCube 2 (or at least SugarCube 1) because modern web-browsers have changed since the vanilla story formats were last maintained and those story formats aren't 100% compatible with all modern web-browsers.

I haven't used Stream's Greenworks API before but the code example you included with your question is Javascript code, not TwineScript and that code consists of two parts:

1. Code to initialise the Greeworks Javascript API.
I would suggest that code like that should be executed within your story's main script tagged special Passage, as you generally only want to initialise an API once and not each time you want to use that API.

2. Code to activate a particular achievement.
This code would most likely need to be directly or indirectly called from within a Passage, how you do that depends on your story format and could be either:

a. A valid script element if using a vanilla story format.

b. A <<script>> macro if using SugarCube 2.x (or even 1.x)

As I don't have access to Greenworks I can't give you tested code examples, but maybe someone else will be able to do that.

by (710 points)
edited by
I am so sorry. I use SugarCube &ndash; v2.20.0 and I did try that code in a Script passage, but without a special tag and it give me a error message about ''require not defined''.
by (63.1k points)

Did you rebuild your app in nw.js before testing? (Basic, I know, but it's easy to forget things). If you're using nw.js, require should be available. 

 I did try that code in a Script passage, but without a special tag

Can you be more specific? A script passage should always have the special script tag in Twine 1, so I'm not sure what you mean here. Do you mean you didn't include the script tag or that you didn't include any additional tags? 

by (159k points)

warning: I don't have Greenworks installed and I haven't setup a NW.js project to test your code.

and it give me a error message about ''require not defined''.

Did you receive that error after:
a. Re-building your NW.js application and running it
b.,Trying to test the Twine 1.x project via the Test menu option and a web-browser.

If A then does your package.json file contain a main entry that references the http://localhost/ URL?, if so then this issue may interest you.

If B then as Greenworks is a node plugin I am not sure you can use it directly from within a web-browser like that, but even if you can you would still need access to a require() function like that defined by the RequireJS Javascript library.

by (710 points)
edited by

I get the error by testing in web browser and rebuilding with NW.js. For the Script passage, the script tag was there. I did not put any other tags. I&rsquo;ll check the documentation and run a couple more tests.

UPDATE: After I put this code in a Script passage and build the game in nw.js, I do not have a ‘require is not defined’ error. That’s good.

var greenworks = require('./greenworks');

if (greenworks.init())
  console.log('Steam API has been initalized.');

But when I try the script code for the achievements, inside the story passages, I got this error: ‘’<<script>> bad evaluation greenworks in not defined’’.

<<script>>greenworks.getAchievement("start")<</script>>

 

by (68.6k points)

You're getting the latest error because of how you declared greenworks, which is as a locally scoped variable.  If you need it to be accessible in other scopes, then you'll either need to make it an auto-global (i.e. a property of window) or assign it to the setup object.

In this case, using NW.js, I'd probably recommend using the setup object.

 

Example - setup object (recommended)

You'd initialize it like so:

setup.greenworks = require('./greenworks');

Using it within a <<script>> invocation:

<<script>>setup.greenworks.getAchievement('start')<</script>>

 

Example - auto-global

You'd initialize it like so:

window.greenworks = require('./greenworks');

Using it within a <<script>> invocation:

<<script>>greenworks.getAchievement('start')<</script>>

 

by (710 points)

With the setup object, I got the following error after the build with NW.js (the Steam API is initialized). ''Error: <<script>> Bad evaluation, bad arguments.'' (Maybe I should fully build it on Steam?)

by (68.6k points)
edited by

If you received an error about "bad arguments", then that's likely coming from Greenworks getAchievement() method itself.

Looking at their achievement API docs (see: https://github.com/greenheartgames/greenworks/blob/master/docs/achievement.md), you are calling it with too few arguments.  It requires, at least, the achievement name and a success callback (function)—it may also take an optional error callback.

You also seem to be calling the wrong method regardless.  The getAchievement() method gets the status of an achievement.  To activate/unlock an achievement you want the activateAchievement() method, which has similar argument requirements.

Try something like the following:

<<script>>
setup.greenworks.activateAchievement(
	'start',
	function () {
		console.log('Achievement "start" activated.');
	},
	function (error) {
		console.log('Error: achievement "start" NOT activated; reason:', error);
	}
);
<</script>>

 

by (159k points)

 Bad evaluation, bad arguments.

Based on the Greenworks Achievement related documentation the getAchievement() function needs at least two parameters:
1. The name of the achievement.
2. The call-back function that getAchievement() will call once it has successfully queried Stream.

eg. Something like the following pseudo code (not tested)

<<script>>
	greenworks.getAchievement('start', function (is_achieved) {
		if (is_achieved) {
			console.log('Start achievement was achieved.');
		} else {
			console.log('Start achievement has not been achieved yet.');
		}
	});
<</script>>

 

by (710 points)

This code work perfect and my test achievement unlock! Thank you so much! Proof image : http://hpics.li/5f1e6bd

<<script>>
setup.greenworks.activateAchievement(
	'start',
	function () {
		console.log('Achievement "start" activated.');
	},
	function (error) {
		console.log('Error: achievement "start" NOT activated; reason:', error);
	}
);
<</script>>

 

...