Support

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

Array problem in Ruby - am I missing something??

For general discussion related FlowStone

Re: Array problem in Ruby - am I missing something??

Postby tor » Wed Jan 23, 2013 7:39 pm

Ouch.. when I reopened this file now, I get an error:
undefined method for 'each' nil........
Then I just removed a comment from the code the error disappeared.
tor
 
Posts: 114
Joined: Fri Sep 24, 2010 5:54 pm

Re: Array problem in Ruby - am I missing something??

Postby trogluddite » Wed Jan 23, 2013 8:07 pm

Ah, yes I've seen that before.

Ruby calls 'init' when FS starts, or you load a schematic - but that happens before the 'green' schematic starts, and before the values saved with the file get read in. This means that some input values may not be defined when 'init' runs for the first time - your '@px', '@py' and '@width' variables in this case.
When you edit the code in any way, 'init' gets run again to make sure the code is refreshed - and this time will always run properly because the input values are now all present and correct.
So 'init' should only be used for code that doesn't have to read the input values.

Simplest way to deal with it is this...

Give 'init' a different name, e.g. 'makeData' - so that there's no 'auto-run' at startup. Then call 'makeData' before refreshing the screen to make sure that the values are all defined.
On its own, that works, but re-making the array for every redraw will use up CPU for no reason, redefining the same things over and over.
To stop that from happening, you can put in a test to make sure that your new 'makeData' method only runs once...
Code: Select all
makeData unless @buttons

This works because of the way that Ruby decides true or false for 'if' and 'unless' statements...
'nil' or 'false' => false
Any other value => true
So, if @buttons has been defined already that counts as true, so 'makeData' won't run - if it isn't defined, it will return 'nil', and 'makeData' gets called.

NB) Something to be careful of !!..
Inside Ruby, the number zero counts as "there is a value, and it's zero" - so zero evaluates as true. Not like 'green ' bools' where zero is false. So if you want zero to count as false, be sure to test for it explicitly.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Array problem in Ruby - am I missing something??

Postby tor » Wed Jan 23, 2013 8:28 pm

hm... a lot to digest :)

for now i just removed the def init method

it solved as you told the update problem. also the error i got due to the late greens as you also said

thank you :)
tor
 
Posts: 114
Joined: Fri Sep 24, 2010 5:54 pm

Re: Array problem in Ruby - am I missing something??

Postby tor » Wed Jan 23, 2013 10:40 pm

This code makes rows for the 'button_values[:y] ' but I am strugling to understand/write the code for 'button_values[:x]'. I can not make it wrap the index number back to zero at each row as desired. Sorry for all the noob Q's coming in a row her the last days.

Code: Select all
arrays=@arrayw*@arrayh-1
@buttons = Array.new
(0..arrays).each do |index|
   button_values = Hash.new
   button_values[:x] = index*(@width+@px)+@px #NEED TO CHANGE THIS
#So I get the rows wrapped, kind of like:
#      button_values[:x] = index-(index>@arrayw)&@arrayw #ie index 0-12 would be index (0-3)*3
#I also see that doin this kind of operation on 'index' will screw up other things.
#But you get the idea?
   button_values[:y] = index/@arrayw*(@height+@py)+@py
   button_values[:state] = 0
   @buttons[index] = button_values
   end
tor
 
Posts: 114
Joined: Fri Sep 24, 2010 5:54 pm

Re: Array problem in Ruby - am I missing something??

Postby tor » Wed Jan 23, 2013 11:08 pm

Here you have the fsm file. I would love to get some help in this project so any one please feel free to use it for your own projects and modify it as you please. Credits so far go to (mostly) Trogluddite and some me.
Attachments
Button_Array_v2.2.fsm
(2.82 KiB) Downloaded 987 times
tor
 
Posts: 114
Joined: Fri Sep 24, 2010 5:54 pm

Re: Array problem in Ruby - am I missing something??

Postby trogluddite » Thu Jan 24, 2013 2:00 am

Looks like you need a modulus...
Assuming that you are numbering left->right then top->bottom, you have the y right - just an integer divide. What you need for the x position is the remainder from that divide - the 'left over' part of the result. And that's what he modulus function does.
The symbol is '%', and you place it in the maths just like a divide - here's an example how you would use it in your code...
Code: Select all
button_values[:x] = index % @arrayw * (@width+@px)+@px   # Modulus
button_values[:y] = index / @arrayw * (@height+@py)+@py   # Divide


tor wrote:Sorry for all the noob Q's coming in a row her the last days

No need to apologise - I have asked many myself over the years!
One of the best things about SM was the amazing collection of modules and code built up on the forum - hundreds of tools, effects, code routines and useful examples - and that's probably the biggest thing missing from FS at the moment, particularly for the new stuff like Ruby. The dev's wouldn't get any updates done if they tried to show an example of everything possible - so I'm up for sharing as much as possible, and throwing mad ideas about, so we can build up the forum to be as good as the SM site (though I will never forget she was my first true love! :lol: )

The buttons are coming along beautifully, it's a really nice design, and plenty of customisable options too - it will be a very useful module I think. "Radio buttons" are one of those things that come up regularly on the forums - now you won't need to build anything - just type in how many you want, and all done!

If we could get a good collection of nice GUI stuff like this together, maybe we could get the dev's to make it one of the official "module packs" for the main download section - so that all the good stuff doesn't get buried under hundreds of threads.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Array problem in Ruby - am I missing something??

Postby tor » Thu Jan 24, 2013 6:56 am

Great :)

So far so good:
Button_Array_v2.4.fsm
(3.79 KiB) Downloaded 1061 times
tor
 
Posts: 114
Joined: Fri Sep 24, 2010 5:54 pm

Re: Array problem in Ruby - am I missing something??

Postby tor » Thu Jan 24, 2013 8:17 am

Still dont quite understand the alternatives to 'def init' in the first code part. I have made some attempts like just replacing 'init' with 'makeData' andwrap the 'def init' inside a 'def event' but I have not yet succeded and I have made some attempts to add a redraw at different places with no luck.

Guess I still have a LOT to learn :mrgreen:
tor
 
Posts: 114
Joined: Fri Sep 24, 2010 5:54 pm

Re: Array problem in Ruby - am I missing something??

Postby tor » Fri Jan 25, 2013 4:14 am

I am trying to change the active mouse area from a circle to a rounded rectanle that follow the shape of the buttons. But my try gives an error. I tried to search google for an answer but no luck.
The original part of the code:
Code: Select all
def clickedButton?(x,y,button)
   button_x = button[:x]
   button_y = button[:y]
   button_area = [button_x,button_y,@width-2*@frthick,@height-2*@frthick]
   circle = GraphicsPath.new
   circle.addEllipse(button_area)
   return circle.isVisible([x,y])
end

My attampt to change the shape:
Code: Select all
def clickedButton?(x,y,button)
   button_x = button[:x]
   button_y = button[:y]
   button_area=[button_x,button_y,@width-2*@frthick,@height-2*@frthick]
   activeArea = GraphicsPath.new
   activeArea.addRoundRect activeArea,button_area,@round
   return activeArea.isVisible([x,y])
end


NoMethodError: (in method 'isInMousePoint'): undefined method 'addRoundRect' for #<GraphicsPath:0xad5f1ec

I presumed the method addRoundRect was sort of like drawRoundRect. But it is not.

I am also looking for a soultion for a second button state that is controlled by an external array (info from a sequencer) so the user can see the beat position. Yes, I am building this because my goal is a pattern sequencer :)
Atm the seq state input is an Int array but in time it will be a ruby connection from an actual sequencer that I think also will be handling the preset functionality when I get there (somewhere in the year 2034 or so).

Problem I have met so far is that if I add a state2 in the first part of the code state 1 gets reset each time i click a button. State2 then only update when I click a button or do a change in the scematic.

Must admit I do not fully understand the event and redaw systems in ruby :oops:
Last edited by tor on Fri Jan 25, 2013 4:38 am, edited 2 times in total.
tor
 
Posts: 114
Joined: Fri Sep 24, 2010 5:54 pm

Re: Array problem in Ruby - am I missing something??

Postby tor » Fri Jan 25, 2013 4:24 am

Here is the fsm file:
Button_Array_v2.6.fsm
(4.59 KiB) Downloaded 1025 times
tor
 
Posts: 114
Joined: Fri Sep 24, 2010 5:54 pm

PreviousNext

Return to General

Who is online

Users browsing this forum: No registered users and 34 guests

cron