accessing table values via event listener of button/object

Hello,

I’m stuck trying to accomplish something that is probably stupidly easy…

As per title of post… i have a table containing some buttons (lots more then in the code below).

…user presses one of them and code needs to load an associated image, who’s name is stored in the table together with the object (button) related to it.

Keep getting “nil” attemps :frowning:

local Buttons ={

[1] = {button=display.newCircle(hor.w01,vert.h1, 20),buttonName=“PA1”,strokeWidth=1,toLoad=“p_a_1”},

[2] = {button=display.newCircle(hor.w03,vert.h1, 20),buttonName=“PA3”,strokeWidth=1,toLoad=“p_a_3”},

[3] = {button=display.newCircle(hor.w05,vert.h1, 20),buttonName=“PA5”,strokeWidth=1,toLoad=“p_a_5”}}

local function seatSelector( event )

     --…do loads of stuff on the objects etc etc…

     loadImageFunction (event.target.toLoad) --<---- how to access the “toLoad” string attached to the event target?

end 

local function setEventListener()

     for i =1 , #seatButtons do

     seatButtons[i].button:addEventListener( “tap”, seatSelector )

     end

end

setEventListener()

this has stomped me all day… 

You would have to do event.target[1] (or 2 or 3).toLoad:

event.target[2].toLoad&nbsp;

Unless your loadImageFunction handles this.

It seems your setEventListener deals with this.

What error message are you getting, is it pointing to a specific line? Please post it.

I did this:

local table = { {name = "W.D. Gaster", age = 9999999}, {name = "Elizabeth", age = 47} } print(table[2].age)

And it worked fine.

See if this helps you:

https://coronalabs.com/blog/2011/06/21/understanding-lua-tables-in-corona-sdk/

Thanks but your suggestion does not solve my dilemma (at least within my understanding) also becouse the code i posted is just a sample. In reality the Buttons array contains hundreds of objects and other strings attached to them (like the toLoad one). Thus the need to access them dinamically. I tried reversing the table but that created even more problems. I basically need to find the index number of the row in the table containing the object referenced in the event.target pointer… Looking forward to giving myself a well deserved facepalm :slight_smile:

Try 

local seatButtons={ [1]&nbsp;=&nbsp;{button=display.newCircle(hor.w01,vert.h1, 20),buttonName="PA1",strokeWidth=1,toLoad="p\_a\_1"}, [2]&nbsp;=&nbsp;{button=display.newCircle(hor.w03,vert.h1, 20),buttonName="PA3",strokeWidth=1,toLoad="p\_a\_3"}, [3]&nbsp;=&nbsp;{button=display.newCircle(hor.w05,vert.h1, 20),buttonName="PA5",strokeWidth=1,toLoad="p\_a\_5"}} local function&nbsp;seatSelector( event ) &nbsp; &nbsp; &nbsp;--...do loads of stuff on the objects etc etc... &nbsp; &nbsp; &nbsp;loadImageFunction (event.target.toLoad) --\<---- how to access the "toLoad" string attached to the event target? end&nbsp; local function setEventListener() &nbsp; &nbsp; &nbsp;for i =1 , #seatButtons do &nbsp; &nbsp; &nbsp;seatButtons[i].button:addEventListener( "tap",&nbsp;seatSelector&nbsp;) &nbsp; &nbsp; &nbsp;seatButtons[i].button.toLoad = seatButtons[i].toLoad &nbsp; &nbsp; &nbsp;end end

It seems you use Buttons instead of seatButtons.

After some tinkering with your suggestion i got it to work…unfortunately i am not really sure why ! :slight_smile:

I am not sure i understand what adding >> seatButtons[i].button.toLoad = seatButtons[i].toLoad << to the listener does “internally”.

I’m happy it works now but i’d be really glad if you could elaborate on it so i can actually learn something :slight_smile:

Thanks!

You would have to do event.target[1] (or 2 or 3).toLoad:

event.target[2].toLoad&nbsp;

Unless your loadImageFunction handles this.

It seems your setEventListener deals with this.

What error message are you getting, is it pointing to a specific line? Please post it.

I did this:

local table = { {name = "W.D. Gaster", age = 9999999}, {name = "Elizabeth", age = 47} } print(table[2].age)

And it worked fine.

See if this helps you:

https://coronalabs.com/blog/2011/06/21/understanding-lua-tables-in-corona-sdk/

Thanks but your suggestion does not solve my dilemma (at least within my understanding) also becouse the code i posted is just a sample. In reality the Buttons array contains hundreds of objects and other strings attached to them (like the toLoad one). Thus the need to access them dinamically. I tried reversing the table but that created even more problems. I basically need to find the index number of the row in the table containing the object referenced in the event.target pointer… Looking forward to giving myself a well deserved facepalm :slight_smile:

Try 

local seatButtons={ [1]&nbsp;=&nbsp;{button=display.newCircle(hor.w01,vert.h1, 20),buttonName="PA1",strokeWidth=1,toLoad="p\_a\_1"}, [2]&nbsp;=&nbsp;{button=display.newCircle(hor.w03,vert.h1, 20),buttonName="PA3",strokeWidth=1,toLoad="p\_a\_3"}, [3]&nbsp;=&nbsp;{button=display.newCircle(hor.w05,vert.h1, 20),buttonName="PA5",strokeWidth=1,toLoad="p\_a\_5"}} local function&nbsp;seatSelector( event ) &nbsp; &nbsp; &nbsp;--...do loads of stuff on the objects etc etc... &nbsp; &nbsp; &nbsp;loadImageFunction (event.target.toLoad) --\<---- how to access the "toLoad" string attached to the event target? end&nbsp; local function setEventListener() &nbsp; &nbsp; &nbsp;for i =1 , #seatButtons do &nbsp; &nbsp; &nbsp;seatButtons[i].button:addEventListener( "tap",&nbsp;seatSelector&nbsp;) &nbsp; &nbsp; &nbsp;seatButtons[i].button.toLoad = seatButtons[i].toLoad &nbsp; &nbsp; &nbsp;end end

It seems you use Buttons instead of seatButtons.

After some tinkering with your suggestion i got it to work…unfortunately i am not really sure why ! :slight_smile:

I am not sure i understand what adding >> seatButtons[i].button.toLoad = seatButtons[i].toLoad << to the listener does “internally”.

I’m happy it works now but i’d be really glad if you could elaborate on it so i can actually learn something :slight_smile:

Thanks!