::Passageheader (it should be included in the prerender script)
<<if $garlicallergy=true>>\ <<if tags().indexOf("garlic") > -1>>\ There is some garlic here! You've lost \ <<set $hplost to -= either(1,2,3)>>\ <<set $hp -= $hplost>>\ <<print $hp>> health points. <br> <<endif>> <<endif>>
There are some problems with that code.
Errors: [list type=decimal] It's doing an, unwanted, assignment in the first <<if>>, when it should be doing an equality test. The <<set $hplost>> bit includes both Twine and JavaScipt assignment operators, back to back "to -=". It's printing the total HP, instead of the intended HP loss.
Fixed code:
<<if $garlicallergy eq true>>\ <<if tags().indexOf("garlic") neq -1>>\ There is some garlic here! You've lost \ <<set $hplost to either(1,2,3)>>\ <<set $hp -= $hplost>>\ <<print $hp>> health points.\ <<endif>> <<endif>>
Nitpicks: [list type=decimal] There's no need two <<if>> macros for what that code is doing. Simply use a logical and to join the two cases. The random() function is more efficient than the either() function if all that's wanted is simply a random number out of a range. The status message is broken up, by <<set>>, complicating it for no real purpose.
Better code:
<<if ($garlicallergy eq true) and (tags().indexOf("garlic") neq -1)>>\ <<set $hplost to random(1,3); $hp -= $hplost>>\ There is some garlic here! You've lost <<print $hplost>> health points. <<endif>>
Guys (everyone, regardless of gender) I want to thank you all for being so helpful and indulgent with your help examples. I'm itching to work on my Twine project (I'm currently occupied with an Inform7 ShuffleComp entry due by end of month) now that I'm starting to get the concepts.
Had a general question about this line:
<<if ($garlicallergy eq true) and (tags().indexOf("garlic") neq -1)>>\
Are tags (or specifically (tags().indexOf("sometag") ) in passages always considered to equal -1 if they are not present in the current passage?
::Passageheader (it should be included in the prerender script)
meaning I have a passage called something like CheckGarlic with the mechanics.
And then in the passage called "prerender" (tagged as a script?) I call this passage <<display CheckGarlic>>
This would fire before the passage is displayed and put any output at the top of the passage?
Similarly - if this stuff is in "postrender" (I think that's what it's called?) it would put it at the end of the passage display? (for example if I wanted to add a choice that triggers off the garlic mechanism at the bottom).
Sorry if these are confused dumb questions, I know just enough about code to be dangerous.
<<if ($garlicallergy eq true) and (tags().indexOf("garlic") neq -1)>>\
Are tags (or specifically (tags().indexOf("sometag") ) in passages always considered to equal -1 if they are not present in the current passage?
Yes.
Calling the tags() function without arguments returns an array consisting of the current passage's tags. Calling the Array object's indexOf() static method returns the index of the first element matching the given value within the array, or -1 if it is not found.
So, tags().indexOf("garlic") searches the current passage's tags for the value "garlic", returning its index within the tags or -1 if it's not found. Thus, if tags().indexOf("garlic") neq -1 is true, then "garlic" was found among the current passage's tags.
Hanon wrote:
::Passageheader (it should be included in the prerender script)
meaning I have a passage called something like CheckGarlic with the mechanics.
And then in the passage called "prerender" (tagged as a script?) I call this passage <<display CheckGarlic>>
This would fire before the passage is displayed and put any output at the top of the passage?
Close. In some script passage, titled whatever you want, you'd put some code like this:
prerender["autoGarilcCheck"] = function (content, task) { new Wikifier(content, '<<display "CheckGarlic">>'); };
Which creates a new prerender task that calls <<display "CheckGarlic">> before the passage is rendered and puts any output at the top of the passage.
Hanon wrote:
Similarly - if this stuff is in "postrender" (I think that's what it's called?) it would put it at the end of the passage display? (for example if I wanted to add a choice that triggers off the garlic mechanism at the bottom).
Yes. A task, similar to the above, added to the postrender object will execute after the passage has been rendered and will put any output at the bottom of the passage.
My end goal is to make a script that updates the content of another div when any link in the passages area is clicked.
But for now, I'm hung up on something that's likely very simple. How do I select <a> elements in the #passages div with jQuery? I've been using .css to check if I'm selecting them by changing the color to red.
My understanding is $("#passages a") should select all <a> elements within div#passages. I tried some different things, too. Here's a list:
Guess I'm overlooking something very trivial. I'm stumped.
The last two examples made the text color of the links in the menu bar red.
Selecting the "link-internal" class isn't what I'm after, but at the very least I expected that one to work. I'm looking at the html of the compiled file, and the <a> elements in div#passages all have class="link-internal".
How do I select <a> elements in the #passages div with jQuery? I've been using .css to check if I'm selecting them by changing the color to red.
From where are you trying to select them? You cannot do it within the passage (it's a chicken/egg problem). Try within the PassageDone special passage.
I'd suggest something like:
$(".link-internal").css("color", "red");
Which will only select links within the game/story itself, regardless of their element type. It will not select links to external resources or anchor elements not used as links.
I was trying to do it from a passage tagged with "script" so yeah, it was the chicken/egg thing.
Not the chicken/egg problem I was referring to, but yes, that's another chicken/egg moment. Normally, all script tagged passages are executed once at startup. You, generally, use them to setup custom macros, utility functions, make various modifications, etc.
Dynamic, in-progress scripting is generally run via the prerender/postrender task objects (in both the vanilla headers and SugarCube) and/or the PassageReady/PassageDone special passages (in SugarCube only).
Let me explain how I do this inventory-time thingy and maybe you can use it also (or propose a better solution). The basic concept is that I mark passage types with tags. A passage can be an entrance of an area (tagged:area) leading to multiple rooms, it can be a room (tagged: room) within that area, it can be an object's description (tagged:object) or anyhting else. I have a variable called $current to track the status of all those types so the setup is something like this:
Comments
There are some problems with that code.
Errors:
[list type=decimal]
It's doing an, unwanted, assignment in the first
<<if>>
, when it should be doing an equality test.The <<set $hplost>> bit includes both Twine and JavaScipt assignment operators, back to back "
to -=
".It's printing the total HP, instead of the intended HP loss.
Fixed code:
Nitpicks:
[list type=decimal]
There's no need two
<<if>>
macros for what that code is doing. Simply use a logicaland
to join the two cases.The
random()
function is more efficient than theeither()
function if all that's wanted is simply a random number out of a range.The status message is broken up, by
<<set>>
, complicating it for no real purpose.Better code:
Had a general question about this line:
<<if ($garlicallergy eq true) and (tags().indexOf("garlic") neq -1)>>\
Are tags (or specifically (tags().indexOf("sometag") ) in passages always considered to equal -1 if they are not present in the current passage?
::Passageheader (it should be included in the prerender script)
meaning I have a passage called something like CheckGarlic with the mechanics.
And then in the passage called "prerender" (tagged as a script?) I call this passage <<display CheckGarlic>>
This would fire before the passage is displayed and put any output at the top of the passage?
Similarly - if this stuff is in "postrender" (I think that's what it's called?) it would put it at the end of the passage display? (for example if I wanted to add a choice that triggers off the garlic mechanism at the bottom).
Sorry if these are confused dumb questions, I know just enough about code to be dangerous.
Yes.
Calling the
tags()
function without arguments returns an array consisting of the current passage's tags. Calling the Array object'sindexOf()
static method returns the index of the first element matching the given value within the array, or -1 if it is not found.So,
tags().indexOf("garlic")
searches the current passage's tags for the value "garlic", returning its index within the tags or -1 if it's not found. Thus, iftags().indexOf("garlic") neq -1
is true, then "garlic" was found among the current passage's tags.Close. In some
script
passage, titled whatever you want, you'd put some code like this: Which creates a newprerender
task that calls<<display "CheckGarlic">>
before the passage is rendered and puts any output at the top of the passage.Yes. A task, similar to the above, added to the
postrender
object will execute after the passage has been rendered and will put any output at the bottom of the passage.My end goal is to make a script that updates the content of another div when any link in the passages area is clicked.
But for now, I'm hung up on something that's likely very simple. How do I select <a> elements in the #passages div with jQuery? I've been using .css to check if I'm selecting them by changing the color to red.
My understanding is $("#passages a") should select all <a> elements within div#passages. I tried some different things, too. Here's a list: Guess I'm overlooking something very trivial. I'm stumped.
The last two examples made the text color of the links in the menu bar red.
Selecting the "link-internal" class isn't what I'm after, but at the very least I expected that one to work. I'm looking at the html of the compiled file, and the <a> elements in div#passages all have class="link-internal".
From where are you trying to select them? You cannot do it within the passage (it's a chicken/egg problem). Try within the
PassageDone
special passage.I'd suggest something like: Which will only select links within the game/story itself, regardless of their element type. It will not select links to external resources or anchor elements not used as links.
I was trying to do it from a passage tagged with "script" so yeah, it was the chicken/egg thing.
Not the chicken/egg problem I was referring to, but yes, that's another chicken/egg moment. Normally, all
script
tagged passages are executed once at startup. You, generally, use them to setup custom macros, utility functions, make various modifications, etc.Dynamic, in-progress scripting is generally run via the
prerender
/postrender
task objects (in both the vanilla headers and SugarCube) and/or thePassageReady
/PassageDone
special passages (in SugarCube only).And I'm getting problems specifically the error:
Apologies! There is a technical problem with this game. You may be able to continue, but some parts may not work properly.
Error [PassageReady]: <<set>>: bad expression: Can't find variable: passage
I am actually using the visual editor as opposed to Tweecode. Is that the problem?
Thank you, this totally worked. I didn't realize I was an update behind!