I want to use JS and not twinescript, so I chose snowman.
Unfortunately I can't seem to make an object in the Story JavaScript which I can then access in any passage.
This seems to me the most basic thing we need to create player stats or inventories.
Is there a way to really use JS with Twine?
Comments
Variables (and some other things) you create in the Story Javascript area are not Gobally available to the rest of the story unless you make them so, and one way to do this is by adding the variables that need to be accessed globally to the browser's window object.
As an example add the following to the Story Javascript area of a Snowman 2 story: ... and then add the following to the first/main passage:
An object based example (equivalent to greyelf's)
Put the following in the Story JavaScript: And the following to a story passage:
Non-object based example (similar to greyelf's)
Put the following in the Story JavaScript: And the following to a story passage:
One thing I was missing is the = to begin the code after <%.
ps - I didn't realise there is a Snowman 2. It has 1.1 in my copy of Twine. I'll have to look into that...
Since Snowman uses _.template(), you have three different template hooks you can use within normal passages (not Story JavaScript). For example:
There are only two versions of Snowman, 1.0 and 1.1.
Snowman 1.1: Also known as Snowman 2, which should be read as Snowman (for Twine) 2, not Snowman (version) 2.
Snowman 1.0: Also known as Snowman 1.4, which should be read as Snowman (for Twine) 1.4.
As noted above, the Snowman versions are 1.0 (for Twine 1.4) and 1.1 (for Twine 2). And, yes, both versions include the jQuery and Underscore libraries.
and in the passage -
2. story.state.… works. Did you follow my example exactly?
For example, if you did the following in your Story JavaScript: Then in your passage you'd use: Note the s. before myObject.
Interestingly, there's a video on Youtube which said never use window. and instead said that document. should be used.
As to the YouTube video. There are probably more people on the internet giving out bad advice than there are doing the opposite, so I'm not surprised. I'm not saying that most people want to give bad advice, but simply that many, in their exuberance to help others (or to show off), try to explain things that they themselves don't have a firm (or any, really) grasp on.
Most of these instances probably have their roots in Cargo Cult Programming, an unfortunate, but real, phenomenon.