tmedwards / sugarcube-2

SugarCube is a free (gratis and libre) story format for Twine/Twee.
https://www.motoslave.net/sugarcube/2/
BSD 2-Clause "Simplified" License
177 stars 41 forks source link

radiobutton visual bug with for loops #76

Closed gavinrobertkemp closed 3 years ago

gavinrobertkemp commented 4 years ago

Hello,

Their a visual bug where if you try and assign values to array using a for loop, visually only 1 button can be checked. for example: <<set _teams = ["1","2","0"]>> <<for _memb range $group>> <<nobr>> <<capture _memb>> <<print _memb.name>><<radiobutton "_memb.team" "1">><<radiobutton "_memb.team" "2">><<radiobutton "_memb.team" "0">> <</capture>> <</nobr>> <</for>> this code is supposed to let me assign members of the group to 1 of 3 teams. this works for the assignment of the value but visually only one radio button is shown selected rather than the number equal to the size of the team. image this is because the html name of the button is the same for all the radiobuttons rather than the ones assigned to each individual as see in this image. image

the simplest solution would be to add an index value to the radiobutton to allow the programmer to iterate the the html name of the radiobutton.

greyelf commented 4 years ago

As explained in the <<radiobutton>> macro's description...

Multiple <<radiobutton>> macros may be set up to modify the same variable, which makes them part of a radio button group.

So because you are passing the same receiverName ("_memb.team") to all the <<radiobutton>> instances in your <<for>> loop then all the HTML radio buttons elements being generation belong to the same radio button group.

note: I will assume that the contents of your undefined $group variable looks something like..

<<set $group to [{name: 'victor', team: ''},{name: 'leona', team: ''},{name: 'david', team: ''}]>>

The following example demonstrates one method you can use to give each set of three radio buttons a unique receiverName, which will result in them belonging to a different radio button group than any other set...

<<nobr>>
    <<for _i to 0; _i < $group.length; _i++>>
        <br>
        <<print $group[_i].name>>
        <<set _receiver to "$group[" + _i + "].team">>
        <<radiobutton _receiver "1">>
        <<radiobutton _receiver "2">>
        <<radiobutton _receiver "0">>
    <</for>>
<</nobr>>
tmedwards commented 3 years ago

As noted by @greyelf , the macro assesses a radio button's group by its receiver name. Thus, using the same receiver name results in the same group.

Due to various issues, mostly centering around the argument list, that's not going to change in v2. That's also not going to change, as the default behavior, in v3, though I should be able to add an optional way to manually specify the group so that workarounds like greyelf's aren't necessary.

tmedwards commented 3 years ago

Closing this in favor of the v3 issue: https://github.com/tmedwards/sugarcube-3/issues/26