User Tools

Site Tools



The «radio» macro lets you offer your reader with a familiar radio button input. While a <<checkbox>> set lets the reader make multiple choices, radio buttons are mutually exclusive–your reader can choose only one option.

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:

  Would you like to see some radio buttons?
  <<radio $choice "yes">>
  <<radio $choice "no">>
  <<radio $choice "maybe">>
  <<button [[Go on|go_on]]>>

Creates this set of options:

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

You see each input has a value–in this example, “yes,” “no,” and “maybe.” These are what will be displayed to the reader AND stored in the variable ($choice) to be used in the next passage.

Thus if a reader chooses “maybe” and clicks the button “Go on,” it's the same as telling Twine this:

  <<set $choice = "maybe">>

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

          <td>Sound?</td><td><<radio $sound "yes">></td><td><<radio $sound "no">></td>
          <td>Color effects?</td><td><<radio $color_effects "yes">></td><td><<radio $color_effects "no">></td>
  <<button [[Begin the Game|Begin]]>>

This code sample creates this:

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

However if you're using the optional SugarCube story format, this option exists. You can find the SugarCube radio button documentation here.

One option to work around this in Twine natively is to initialize your variable to the default value and offer the reader only the other choice(s):

  <<set $sound = "no">>\
  <<set $color_effects = "no">>
          <td>Sound?</td><td></td><td><<radio $sound "yes">></td>
          <td>Color effects?</td><td></td><td><<radio $color_effects "yes">></td>
  <<button [[Start the Game|start_game]]>>

A disadvantage of this approach is that, once the option is checked, it can't be unchecked back to whatever you set as the default.

Hope this helps. If you have questions, please visit the Twinery forum.

radio.txt · Last modified: 2017/10/09 20:39 (external edit)