Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

[Harlowe] Finding the order in which items are added to an array

So, I have a story in which the order you visit pages is important (at least, the first visit). So if you go to page 1, page 6, page 4, and page 5 in that order, it's different than going to 1, 2, 3, 4, 5.

The first thing I tried was adding passage names to an array for the pages that mattered for the count, if you once you click on a link to go to the page. Then, let's say the text on page 3 changes if you have been to page 5, before going to page 4. So on page 3:

(if: $passlog.indexOf("page5") is not -1 and is < $passlog.indexOf("page4")[some text] (else:)[other text]

Which simply did not work at all. So I was trying to figure if either .indexOf doesn't work, or if I am using it wrong; both are completely possible in this case. I did find a kind of kludgey workaround-- I created variables to store the number of times I visit the passages ($count1, $count2) in question, and created a third variable for each page ($p1; $p2, et al). So If, say, p1 is in $passlog already, it'll set $p1 to itself; if it is not, then it'll set $p1 to whatever $count1's value is. Then I can compare $p1 and $p2 for basically the same result. The problem is what happens if $p1 and $p2 are equal, which is pretty likely... in fact, the only reason this is working at all right now is because there's a bug in my counting method that is artificially inflating the numbers of later passages.

So... Help?


  • edited August 2015
    There's a few syntax errors in your code.
    There's a bracket missing after "page4"
    When doing multiple conditions you need to provide the left hand side for each condition, even it it's the same (though you can use 'it' to reduce on typing)
    You don't need the second 'is'

    The following will work
    (if: $passlog.indexOf("page5") is not -1 and it < $passlog.indexOf("page4"))[some text] (else:)[other text]

    BTW you've posted this question twice.
  • Urgh, sorry about that. /facepalm.

    unfortunately, it's not working... I could have the wrong assumption though. I figured that it'd find the first instance of "page5" in the array and return that; if "page5" is in the array multiple times. Maybe that's the problem?
  • edited August 2015
    According to Mozilla's docs your assumption is right. indexOf returns the index of the first instance of "page5" in the array. I did a quick test and
    (set: $test to (a: "page5", "page4", "page5"))
    (if: $test.indexOf("page5") is not -1 and it < $test.indexOf("page4"))[works]

    prints 'works' as expected.

    What problems are you getting?
  • One of the techniques used to make the testing of code easier is to break down the thing you are trying to test into into smaller parts, also printing out the current value of $variable can help.
    passlog: (print: $passlog)
    (set: $var1 to $passlog.indexOf("page5"))
    (set: $var2 to $passlog.indexOf("page4"))
    var1: (print: $var1)
    var2: (print: $var2)
    (if: $var1 is not -1 and $var1 < $var2)[some text] (else:)[other text]
  • I was overcomplicating things, and now have gotten it to work. If I tried to directly compare the indexes, it didn't seem to like that, but if I set variables on the actual page (like greyelf does there) it works like a charm. Thanks everyone!
Sign In or Register to comment.