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

For general discussion related FlowStone
Post Reply
User avatar
Parki
Posts: 20
Joined: Fri Feb 27, 2015 11:12 am

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

Post by Parki »

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 722 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
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

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

Post by KG_is_back »

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.
Post Reply