Skip to content

"Cycling Choices": SugarCube (v2.18)#


"Cycling Choices" demonstrates how to create a 'cycling' effect of different choices through clicking on them.

The cycle starts with the use of the <<include>> macro and assumption of \$choicesCount beginning at the number -1. Within the passage "Cycling", the first 'cycle' begins with testing if the variable \$choices exists. If it does not, \$choices and \$choicesCount are set (created) to their initial values.

Next, \$choicesCount is then increased by one to the start value of 0 (the first location of an array in SugarCube) and the position of \$choices is shown based on this.

If the user clicks on the link (created through using the <<linkreplace>> and <<include>> macros) again, future 'cycles' test if \$choicesCount increases beyond the number of values in the \$choices array and resets it to 0.

At the end of every cycle, the currently selected value is stored in the variable \$cyclingResult for future access and usage.



Twee Code#

:: StoryTitle
Cycling Choices in SugarCube

:: Start
Click options to cycle: <<include "Cycling">>

:: Cycling
<<if not $choices>>
  <<set $choicesCount to -1>>
  <<set $choices to ["First", "Second", "Third"]>>

<<set $choicesCount to $choicesCount + 1>>

<<if $choicesCount >= $choices.length>>
   <<set $choicesCount to 0>>

<<set $cyclingResult to $choices[$choicesCount]>>
\<<linkreplace $choices[$choicesCount]>><<include "Cycling">><</linkreplace>>

:: Results

Twee Download

See Also#

Setting and Showing Variables, Modularity