====== <<checkbox>> ======


The <<checkbox>> macro lets you offer your reader with a group of checkboxes to choose from. While a [[<<radio>>]] set lets the reader choose only one option, checkboxes let the reader choose any combination of options--or none at all!

Note that, in order for the value to be stored, there must be a [[<<button>>]] in the passage, and the user must click it.

This code sample:

    What would you like to pack?
    
    <<checkbox $backpack "shoes">>
    <<checkbox $backpack "shirt">>
    <<checkbox $backpack "toothbrush">>
    
    <<button [[Go on|go_on]]>>

Creates this set of options:

{{:twine_checkbox_default.png?nolink|}}

In this example, the variable $choice is the name for this set of checkboxes. You can set this to any variable name you like.

You see each input has a value--in this example, "shoes," "shirt," and "toothbrush." These are what will be displayed to the reader AND stored in the variable ($choice, as a list separated by commas) to be used in the next passage.

Thus if a reader chooses "shoes" and "shirt" and clicks the button "Go on," it's the same as telling Twine this:

    <<set $choice = ["shoes", "shirt"]>>

You'll notice this looks a little different from the usual variable. That's because the values chosen by the reader are stored in the order you provided them in a "list." The way to get stuff back out of a list is by asking for it by its position. In this case:

    $choice[0] is "shoes"
    $choice[1] is "shirt"

Note that the position starts at 0.


As of version 1.4.2, Twine forces a line break between checkboxes. To work around this, you can place your checkboxes in an HTML table:

    <table>
        <tr>
            <td>Sound?</td><td></td><td><<checkbox $sound "dialogue">></td><td><<checkbox $sound "foley">></td><td><<checkbox $sound "music">></td>
        </tr>
        <tr>
            <td>Visual effects?</td><td></td><td><<checkbox $visual_effects "red/green">></td><td><<checkbox $color_effects "gradients">></td><td><<checkbox $visual_effects "flashes">></td></td>
        </tr>
    </table>
    <<button [[Begin the Game|Begin]]>>


This code sample creates this:

{{:twine_checkbox_table.png?nolink|}}


As of Twine version 1.4.2, there is no native option to mark a checkbox as selected before your reader makes their choice.

But if you're using the SugarCube story format, there are additional checkbox options. You can find [[http://www.motoslave.net/sugarcube/docs/#macros-checkbox|SugarCube checkbox documentation here]].

Hope this helps. If you have questions, please visit the [[http://twinery.org/forum/index.php/board,2.0.html|Twinery forum]].