If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
Question about the stock: Switch - Button
7 posts
• Page 1 of 1
Question about the stock: Switch - Button
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?
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
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.
- tor
- Posts: 114
- Joined: Fri Sep 24, 2010 5:54 pm
Re: Question about the stock: Switch - Button
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!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: Question about the stock: Switch - Button
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 now.
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 now.
- tor
- Posts: 114
- Joined: Fri Sep 24, 2010 5:54 pm
Re: Question about the stock: Switch - Button
Great explanation trogluddite!
-
support - Posts: 151
- Joined: Fri Sep 07, 2012 2:10 pm
Re: Question about the stock: Switch - Button
support wrote:Great explanation trogluddite
Thankyou very much!
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
7 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 79 guests