0 votes
asked by (160 points)

Version: Twine 2.2.1  Story Version: Sugarcube 2.21

To begin my story, I have user input in the form of radio button selections. If the player only selects one, it changes the appropriate variables, and the story begins, however, there is no error catching preventing the player from selecting all radio buttons (as if they were check boxes) if the player does this, all variables get changes (as expected) and the story begins. 

Any way to prevent multiple radio buttons from being selected? 

My name is:<br> <<textbox "$name" $name>><br>
<<radiobutton "$Strength" 2>> Factory Worker
<<radiobutton "$Charisma" 2>> Teacher
<<radiobutton "$Reflexes" 2>>  Martial Arts
<<radiobutton "$Perception" 2>>  Detective

I love animals, especially dogs. If I had one, I would name them:<br>
<<textbox "$dogfriend" $dogfriend>>
<<button "Confirm">>
<<set $name to $name.trim()>>
<<if $name is "">>
	<<replace "#name-error">>Please enter a name!<</replace>>
	<<goto "Home">>
<</button>> &nbsp;&nbsp;
<span id="name-error"></span>


1 Answer

+1 vote
answered by (60.4k points)
selected by
Best answer

The radio buttons need to refer to the same variable to be exclusive. So the easiest thing in your case to do would be to have each radio button set a variable, like $bonus, to the name of the variable (without the $ sigil), and do the math on the next passage. For example: 

My name is:<br> <<textbox "$name" $name>><br>
<<radiobutton "$bonus" "Strength">> Factory Worker
<<radiobutton "$bonus" "Charisma">> Teacher
<<radiobutton "$bonus" "Reflexes">>  Martial Arts
<<radiobutton "$bonus" "Perception">>  Detective


—> in the next passage: 
<<set State.variables[$bonus] to 2>>
<<unset $bonus>>

This code should do roughly the same thing as your code, but the player can only select one radio button now. 

commented by (160 points)
Fantastic! works perfectly. Thank you!
Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.