Radio button-like behavior


Greetings OptoFans,

I recently had someone ask me for some sample code to make use of a loop in OptoScript.

In his application, he has several user choices (in an integer table), and wants those options to act like radio buttons. In other words, if one choice is selected, the others are de-selected. He originally solved this with multiple if statements but was looking for a loop option.

Here’s his original code and my suggested loop. I’m hoping you Clever People Out There in OptoLand will have options and/or other suggestions too. Don’t be shy!

Pre-loop before code:

if (ntSource[38]) then 
  ntSource[39]=0;
  ntSource[40]=0;
  ntSource[41]=0:
endif


if (ntSource[39]) then 
  ntSource[38]=0;
  ntSource[40]=0;
  ntSource[41]=0:
endif


if (ntSource[40]) then 
  ntSource[38]=0;
  ntSource[39]=0;
  ntSource[41]=0:
endif


if (ntSource[41]) then 
  ntSource[38]=0;
  ntSource[39]=0;
  ntSource[40]=0:
endif



Mary’s suggestion for how to use a loop instead (using the handy and under-appreciated MoveToNumTableElements command, and assuming you have OPTION_FIRST = 38; OPTION_LAST = 41; or some such defined earlier).

// loop through and clear out all of the other options besides the first one we find set,
// starting at OPTION_FIRST defined earlier
// Note: if you get to OPTION_LAST, you either have none at all set, or none left to clear!


nSourceIndex = OPTION_FIRST;


while (nSourceIndex < OPTION_LAST)  


  if (ntSource[nSourceIndex]) then // we’ve found our selection, first one wins, clear out all the others
    MoveToNumTableElements(/*From*/0, /*Start Index*/nSourceIndex+1, /*End Index*/OPTION_LAST, /*Of Table*/ ntSouce);
    nSourceIndex = OPTION_LAST; // to bail out of the loop, since we’re done
  endif
  
  nSourceIndex = nSourceIndex + 1;
wend



Hooray for loops and more maintainable code!
Disclaimer: I did not test this code, I made a few assumptions here, and sometimes I write bugs. Use at your own risk. :wink:

-OptoMary

If the display is manipulating the ntSource directly what happens when ntSource[38] is true and ntSource[39] (or any of the others) gets selected next in display? There must be more to it than that, a ntLastValue maybe?

I would think about doing it all from PAC Display. Set up multiple “Send Discrete” attributes on each radio item, one to set the selected item and multiple others clear the rest. It would be even easier if doing this at the bit level instead of a table - as you can set the entire integer to zero and then set the selected bit through the Send Discrete attribute in PAC Display or even set the value directly using the power of 2s.

I did actually implement something like this, entirely with visibility settings in PAC Display and using just one i32 to keep track of what item in the table was being selected. If the i32 matched the index of the radio button, visibility = ON, otherwise, visibility = OFF. Of course, pressing the button would set the i32 to that particular index, but there were other places in the strategy that the i32 could be changed as well, so the radio button would always show the actively selected item.

Kind of tedious to build, but now that it is done, copy and paste into other applications isn’t so bad.

I like sensij’s method as well - pretty simple.

sensij - Are you in San Diego? I’ve seen the forum name at other places, but maybe just a coincidence.

I want to up-vote philip’s bit level method / setting the entire thing at once. Sometimes sending multiple values with the same button can get tedious to edit/maintain. I’ve run into a couple issues with them and like the simplicity of one integer for one interlocked task.