Question about the stock: Switch - Button

For general discussion related FlowStone
Post Reply
tor
Posts: 114
Joined: Fri Sep 24, 2010 5:54 pm

Question about the stock: Switch - Button

Post 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?
tor
Posts: 114
Joined: Fri Sep 24, 2010 5:54 pm

Re: Question about the stock: Switch - Button

Post 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 1205 times
User avatar
trogluddite
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Question about the stock: Switch - Button

Post 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! ;)
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
tor
Posts: 114
Joined: Fri Sep 24, 2010 5:54 pm

Re: Question about the stock: Switch - Button

Post 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.
User avatar
support
Posts: 151
Joined: Fri Sep 07, 2012 2:10 pm

Re: Question about the stock: Switch - Button

Post by support »

Great explanation trogluddite!
User avatar
trogluddite
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Question about the stock: Switch - Button

Post by trogluddite »

support wrote:Great explanation trogluddite

Thankyou very much! :D
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
tor
Posts: 114
Joined: Fri Sep 24, 2010 5:54 pm

Re: Question about the stock: Switch - Button

Post by tor »

Like!
Post Reply