Limiting number of checkboxes user can pick

Hi all, relative beginner here (on my third Twine story) working with Sugarcube. I apologize if this is a newbie question, but I’ve spent a lot of time with the documentation and haven’t found an answer yet.

I want to give the player a list of 10 items and let them pick exactly 5 (saving those choices as variables) before advancing to the next passage. I’m planning on doing it with checkboxes right now, but currently, I can’t figure out how to limit the amount they can choose. Any advice on the best way to do this? Thought about using some kind of counter that would make the “next passage” link visible, is that possible?


Which of the following items do you want to put in your backpack? You can only fit five.

Cell Phone

1 Like

You could do this with two passages. One has a list of items each filtered to show only if not selected (or disabled if selected.) Selecting an item sets the variable, increments a counter up to five, and re-displays the same page, now missing the just-selected item or showing it as a non-link.

When the counter hits five, the page will redirect to a new passage listing what’s in the pack with an “undo and start over?” link that resets everything, and a “confirm” link that proceeds.

Here’s AXMA pseudo code, YMMV.

<if $good gte 2><goto ‘confirm’><endif>
Good, fast, cheap: pick TWO. 
<If $good>
Good. (Selected)
[[Good.|select {$good=true}{$counter++}]]
<If $fast>
Fast. (Selected)
[[Fast.|select {$fast=true}{$counter++}]]
<If $cheap>
Cheap. (Selected)
[[Cheap.|select {$cheap=true}{$counter++}]]

Are you happy with your selections? 
<if $good>
<if $fast>
<if $cheap>

[[I am filled with doubt.|select {$good=null}{$fast=null}{$cheap=null}{$counter=0}]]

I have some sample code which shows you how to have a maximum number of checked checkboxes in Twine/SugarCube. (You can click “Jump to Start” in the UI bar to see a bunch of other sample code there as well.)

You should only have to modify it a bit to get it to work for your code.

Hope that helps! :slight_smile:

1 Like

Thanks Hanon and HiEv! Really helpful. I ended up hacking together a much more amateurish, much less elegant solution with linkreplace. Here’s what I did, if you’re interested.


<img src="" width="200" height="200" class="center" alt="Hiker">
Since you only plan to be out a few hours, you carry a small daypack. Which five items do you bring? 
<<set $filter to false>>
<<set $rope to false>>
<<set $headlamp to false>>
<<set $helmet to false>>
<<set $cell to false>>
<<set $map to false>>
<<set $gps to false>>
<<set $plb to false>>
<<set $harness to false>>
Items in Pack = <<set _packweight to 0>><span id="packweight"><<print _packweight>></span>

*<<linkreplace "Headlamp">>Headlamp<<set $headlamp to true>><<set _packweight++>><<replace "#packweight">><<print _packweight>><</replace>><<if _packweight gte 5>><<goto "Story2B">><</if>><</linkreplace>>
*<<linkreplace "Rope">>Rope<<set $rope to true>><<set _packweight++>><<replace "#packweight">><<print _packweight>><</replace>><<if _packweight gte 5>><<goto "Story2B">><</if>><</linkreplace>>
*<<linkreplace "Cell phone">>Cell phone<<set $cell to true>><<set _packweight++>><<replace "#packweight">><<print _packweight>><</replace>><<if _packweight gte 5>><<goto "Story2B">><</if>><</linkreplace>>
*<<linkreplace "Map and compass">>Map and compass<<set $map to true>><<set _packweight++>><<replace "#packweight">><<print _packweight>><</replace>><<if _packweight gte 5>><<goto "Story2B">><</if>><</linkreplace>>
*<<linkreplace "Personal Locator Beacon">>Personal Locator Beacon<<set $plb to true>><<set _packweight++>><<replace "#packweight">><<print _packweight>><</replace>><<if _packweight gte 5>><<goto "Story2B">><</if>><</linkreplace>>
*<<linkreplace "Helmet">><<set $helmet to true>>Helmet<<set _packweight++>><<replace "#packweight">><<print _packweight>><</replace>><<if _packweight gte 5>><<goto "Story2B">><</if>><</linkreplace>>
*<<linkreplace "Water filter">><<set $filter to true>>Water filter<<set _packweight++>><<replace "#packweight">><<print _packweight>><</replace>><<if _packweight gte 5>><<goto "Story2B">><</if>><</linkreplace>>
*<<linkreplace "GPS">><<set $gps to true>>GPS<<set _packweight++>><<replace "#packweight">><<print _packweight>><</replace>><<if _packweight gte 5>><<goto "Story2B">><</if>><</linkreplace>>
*<<linkreplace "Harness">><<set $harness to true>>Harness<<set _packweight++>><<replace "#packweight">><<print _packweight>><</replace>><<if _packweight gte 5>><<goto "Story2B">><</if>><</linkreplace>>


Here's what you're bringing:

<<nobr>><<if $rope is true>><<print "Rope">><br><</if>>
<<if $helmet is true>><<print "Helmet">><br><</if>>
<<if $plb is true>><<print "Personal Locator Beacon">><br><</if>>
<<if $cell is true>><<print "Cell phone">><br><</if>>
<<if $filter is true>><<print "Water filter">><br><</if>>
<<if $map is true>><<print "Map and compass">><br><</if>>
<<if $gps is true>><<print "GPS">><br><</if>>
<<if $headlamp is true>><<print "Headlamp">><br><</if>>
<<if $harness is true>><<print "Harness">><br><</if>><</nobr>>

Are you ready to go?

*[[Yes, let's hike!|Story3A]]
*[[No, I want to repack.|Story2]]
1 Like