Update: The Javascript on this page is now built into Twine 1.4! It is no longer necessary to install it.
I've noticed that a good deal of variable usage in Twine is simply employed to track whether or not you've visited a certain passage previously, or done so a number of times. This script code provides you with a function that can give this value straight, without needing to use the <<set>>
macro.
window.visited = function(e) { var ret,c; e || (e = state.history[0].passage.title); for(ret=c=0; c<state.history.length; c++) { if(state.history[c].passage && state.history[c].passage.title == e) { ret++; } } return ret; }
To use it, use "visited("Passage")
" as a value in a <<set>>
, <<if>>
or <<print>>
macro. It will equal the number of times you've visited the passage whose name is between the quotes and parentheses. For instance:
<<if visited("Store")>>You've been to the store.<<endif>>
<<if not visited("Firepit") and not visited("Maggotpit")>>You've not yet been to the fire pit or maggot pit.<<endif>>
<<if visited("Trap Floor") gt 2>>Your repeated stomping is making the floor weaken.<<endif>>
You've visited the chasm <<print visited("Dark Chasm")>> times.<<endif>>
<<set $count = visited("Jewelers")>>You've visited the Jeweler's <<print $count>> times.<<endif>>
<<if visited($bossPassage)>>If only you'd had this when you'd fought the boss!<<endif>>
<<set $beenToMyHome = visited($myHome)>>
New: If you just put visited()
, then it will count visits to the current passage, without you needing to include the passage's name.
<<set $beenHereBefore = visited()>>
In addition, here is a function called "visitedbefore" which can be used to tell you if a passage has ever, in the course of the game, been visited before another passage.
window.visitedbefore = function(e,f) { var h,c,ret; for(ret=c=0; c<state.history.length; c++) { h = state.history[c].passage; if(h && h.title == e) { return true; } else if (h && h.title == f) { return false; } } return false; }
<<if visitedbefore("Firepit", "Maggotpit")>>First the fire pit, then the maggot pit, now this?!<<endif>>
<<if visited("Armory") and visitedbefore("Firepit", "Maggotpit")>>First the fire pit, then the maggot pit, now this?!<<endif>>
<<if visitedbefore("Dragon Hoard", $myHome)>>You must've left it at home after meeting the dragon.<<endif>>
Some notes:
<<display>>
macro as a visit.visitedbefore()
will return false.