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

Can't add more then one value to array(solved)

For general discussion related FlowStone

Can't add more then one value to array(solved)

Postby Parki » Sat Mar 14, 2015 7:40 am

WIth this one I can add more then one value to array.
----------------------
def init
@v1 = []
end

def event i,v
v2 = @ins[0]

if i == 1
@v1.push v2
end

watch "v1:",@v1
end
-----------------------
But with this I can't code the same but v1 is local. Does it possible make it with local array?
------------------------
def init
@v1 = []
end

def event i,v
v2 = @ins[0]

if i == 1
@v1.push v2
end

watch "v1:",@v1
end
Attachments
Global-local problem with variables.fsm
Fsm with the code.
(1.29 KiB) Downloaded 642 times
Last edited by Parki on Sun Mar 15, 2015 2:02 pm, edited 1 time in total.
I hope my english was understeandable.
Skype id - parkidesu
User avatar
Parki
 
Posts: 20
Joined: Fri Feb 27, 2015 11:12 am

Re: Can't add more then one value to array if the array is l

Postby KG_is_back » Sat Mar 14, 2015 9:45 am

Your problem is, that local variable is initialized new every time the method is called (in case of event method - every time a trigger is received). in the beginning of each event the array is empty and you add one value to it. There is no way to prevent that - you will have to use @array to store values between events (like in your second example). That's simply how ruby works.
Local variable is local to that given method within each Rubyedit. instance variables (@variable) are local to each Ruby edit (you may have multiple Ruby codes with variable "@variable" and each will have its own.
KG_is_back
 
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia


Return to General

Who is online

Users browsing this forum: No registered users and 71 guests