Page 1 of 1

Question about the stock: Switch - Button

Posted: Tue Jan 22, 2013 8:33 pm
by tor
The Switch - Button in FS has a bolean input from the preset module the input is called: setState. But I can not find @setState anywhere in the code.

Where in the code does this appear and and how does it work?

Re: Question about the stock: Switch - Button

Posted: Tue Jan 22, 2013 9:56 pm
by tor
What I want is the same behaviour as in the stock button. Exept I just need it to toggle the "LED on" layer on/off.
Button.fsm
(143.02 KiB) Downloaded 1206 times

Re: Question about the stock: Switch - Button

Posted: Tue Jan 22, 2013 11:15 pm
by trogluddite
tor wrote:Where in the code does this appear and and how does it work?

They've sneakily hidden it away inside the 'event' method....

Code: Select all

def event i,v
   if i==2
      if @on != v
         @on = v
         output 0,@on
      end
   end
   redraw 0
end

Still don't see it? - that's because there's more than one way to get the value of an input...

The 'event' method in the code gets triggered every time any input changes. When there's a change, 'i' gets set to the index number of the input that changed, and 'v' gets set to whatever the new value is, then the code runs.
First thing in this bit of code is "if i==2", so it only ever does much when the third input changes (inputs start from zero) - which is the 'set_value' input. So the new set value ends up in 'v' without ever having to call the input by name - the little bit of code inside the 'if' statement does all the work using 'v' instead of '@set_value'., but the end result is exactly the same.

(NB - The 'redraw 0' is not inside the 'if' statement because it already 'end'ed, so redrawing will happen for any input change, not just the 'set_value'.)

If you have an 'event' method anywhere in your code, then the code will always refer to that whenever an input changes to see what it should do - without 'event', the whole code runs every time for every input whether you want it to or not.
For this control, event only needs to react to that particular input as pretty much everything else is done by the GUI and mouse.

The third way of getting an input value is to use the @ins array - a single array that contains all of the current input values, no matter what type they are. To look up a particular input, you look up the array index...
second_input_value = @ins[1]
Looking up array indexes is not as efficient as just calling a regular variable though, so @ins is probably best used only if you need the inputs in array form for some special chunk of code.

(Oh yes, and there's a fourth way! - if you only have one input, you can get the value from plain old '@in')

That's the biggest trouble with Ruby - too much choice! ;)

Re: Question about the stock: Switch - Button

Posted: Tue Jan 22, 2013 11:49 pm
by tor
I kind of imagined that 'i' was the index of the incoming event values 'v' in serial order.
1,5,4,8,9,7-->Values in serial
0,1,2,3,4,5-->Indexes

But the 'i' is the index of what input.

Thanks for the clarification. I see the :idea: now.

Re: Question about the stock: Switch - Button

Posted: Wed Jan 23, 2013 5:47 pm
by support
Great explanation trogluddite!

Re: Question about the stock: Switch - Button

Posted: Wed Jan 23, 2013 7:26 pm
by trogluddite
support wrote:Great explanation trogluddite

Thankyou very much! :D

Re: Question about the stock: Switch - Button

Posted: Wed Jan 23, 2013 7:31 pm
by tor
Like!