It looks like you're new here. If you want to get involved, click one of these buttons!
<<set $eyeColors to [ "Blue", "Brown", "Gray", "Green", "Hazel", "Red", "Violet" ]>>
<<set $player = { eyeColor : 1 } >>
<tr> <td> Eye Color </td> <td> <<numberpool "$eyeColors.length">> <<numberbox "$player.eyeColor" $player.eyeColor 1 $eyeColors.length>><span id="eye-color"><<print $eyeColors[$player.eyeColor]>></span> <<onchange>> <<replace "#eye-color">><<print $eyeColors[$player.eyeColor]>><</replace>> <</numberpool>> </td> </tr>
Comments
As noted within its documentation, the pool value is modified—as the pool is drained/filled. By using "$eyeColors.length", you're modifying the length of the $eyeColors array, which is the cause of most of your other issues.
An additional issue is that array indices are 0-based, not 1-based.
So, you need to use a copy of the array length for the pool value and to use 0-based indices. I'd suggest using temporary variables here. For example, for the initial value of $player.eyeColor: And for the player-facing code:
I used the numberpool as a a means of selecting the desired index of an associative array. Ideally, I would have liked to been able to display the index's value as opposed to the index position in the numberbox, but it does work quite well.
I appreciate you taking the time to respond to the topic nonetheless and hope it stands to answer other people's questions in the future should they find themselves in similar situations.
If you simply want to give the player a choice of eye color, then <<radiobutton>> works.
If you really want to cycle through the eye color values, then you could do something like the following:
There's also the <<cyclinglink>> macro, which you may find a SugarCube v2 compatible version of in the same place you did the <<numberpool>> set—on SugarCube v2's website (under: Downloads > Add-ons).
I was originally wanting a simple slider as I'm storing the numerical indices of the associative array instead of copying the value each time it gets set since the array will be used in several locations as a constant dataset.
Using the numberpool macro in this case gave me the a desired aesthetic and usability . It was much cleaner than an x amount of links as I've seen in a number of games. The player can easily cycle back and forth through the available options until they settle on their desired choice.
You have given me more options to consider. I was curious about using cyclinglink when I had settled on numberpool.