Hello,
I am trying to build a character creation system, where the player can navigate several options of a specific trait with left and right arrows to choose the right option, something like.
Choose your hobby: Swimming < >
And the player can click "<" and ">" to navigate the options.
I am trying to implement like this (newbie here :))
1) In a passage tagged with "startup" I've created an array and a variable to traverse the array:
(set: $hobby to (array: "Swimming", "Football", "Stamp collector"))
(set: $i = 0)
2) Then, I've created a passage with the following code:
|hobby>[(print: $hobby[$i])] |nav>[(display: "Nav")]
3) And in the "Nav" passage, I've started by placing the following, which works as long as you stay inside the limits of the array (not very useful, of course :))
{
(link-repeat: "< ")[(set: $i to it - 1)(replace: ?hobby)[(text: $hobby[$i])]]
(link-repeat: " >")[(set: $i to $i + 1)(replace: ?hobby)[(text: $hobby[$i])]]
}
4) To make the navigation limited to the size of the array, I was thinking about something like this, but, alas, it doesn't work (this is the code just for the left arrow)
(link-repeat: "< ")[(if: $i == 0)[(set: $i to $hobby's length - 1)](else:)[(set: $i to it - 1)](replace: ?hobby)[(text: $hobby[$i])]]
So the idea is to check the value of the index variable each time there is a click in an arrow. For the left arrow case, if $i is equal to 0, it should change to the last value of the array. If it's not 0, then it should decrease and move towards the beginning of the array.
But when I run the code, the passage starts fine showing element[0] of the array, but when I click on the left arrow I get this error:
☕ Cannot read property 'varref' of undefined►There's nothing before this to do (else:) with.►
I don't really understand why this is happening. I would really appreciate your help!
Thanks
José