Howdy, Stranger!

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

Bug in my cycling link code - strange things happening with arrays

The code below is for a "cycling link": every time you click some text, it changes to the next thing in a list.
(set: $idx to 1)
(set: $a to (a: "Ozymandias","Fred","Harry Potter"))
(set: $myVar to ($a's 1st))

(print: a)

And on the pedestal these words appear:
'My name is (link-repeat: $myVar)[(replace: $myVar)[(set: $idx to it + 1, $myVar to $a's (($idx % $a's length) + 1))$myVar]], king of kings:
Look on my works, ye Mighty, and despair!'

However! While it mostly works, I've noticed that the
(print: a)
line prints out some weird, weird things. After one click, I get "Fred,Fred,Harry Potter" rather than the original list, which shouldn't change. And, if you have multiple cycling links like this in the same passage, the behavior of each is erratic and the text of all of them changes if you click one.

Can anyone spot my bug?

Comments

  • edited February 2016
    Both of your (print: a) examples are missed the $ dollar sign on the variable name, they should be (print: $a)

    When you do something like (print: $a) a copy of the contents of the variable is added to the page, which means that any changes to the variable do not automatically appear on the page. The reverse is also true, any changes to the copy that was displayed on the page do not change the original variable.

    The (replace:) macro works in one of two ways:

    1. (replace: ?namedhook) - Replaces the contents of the named hook with the contents of the (replace:) macro's associated hook.

    2. (replace: "Ozymandias") - Replaces all occurrences of the word "Ozymandias" that appear in the current page with the contents of the (replace:) macro's associated hook.

    The (replace: $myVar) macro call in your example is using option 2, which is why the output of your (print: $a) changes when you click on your cycle link.

    The value stored in your $a variable is not actually changing and you can see this if you add a markup link (to another passage) to the end of your example, and in that other passage you place a (print: $a).

    Creating you own Cycle Link code in Harlowe can be difficult, which is why some people use Furkle's Cycling Link addon
Sign In or Register to comment.