passing parameter to function in event listener

Hi.

i’ve been curious for a while, but is it actually okay, or should i rephrase it, return 100% possibility that giving parameter into listener function will work?

here is the snippet code to help understand my question

local function getGlow(num) return function(event) if "began" == event.phase then buttonTable[num] = display.newImage(group, symbols[num].glow, symPos[num].x, symPos[num].y) elseif "moved" == event.phase then elseif "ended" == event.phase or "cancelled" == phase then buttonTable[num] = display.newImage(group, symbols[num].origin, symPos[num].x, symPos[num].y) count = count + 1 print(count) end end end local function makeButton() --local right = symbols[symbolNum] --load all symbols for i = 1, #symbols do buttonTable[i] = display.newImage(group, symbols[i].origin, symPos[i].x, symPos[i].y) buttonTable[i]:addEventListener('touch', getGlow(i)) //is this will return every time?? end end

That is fine. This is called lua closures and is one of the top features of lua in my opinion.

so you don’t need to wrap it up like this?

--load all symbols for i = 1, #symbols do buttonTable[i] = display.newImage(group, symbols[i].origin, symPos[i].x, symPos[i].y) buttonTable[i]:addEventListener('touch', function() getGlow(i) end) //wrapped it up with a function instead end end

Nope @jonathan, look that get glow returns already wrappex function.

oh, okay then. cool. thanks! :smiley:

No beacuse getGlow(num) return a function so that is fine. The way you wrote it now is not actually going to work for many reasons:

  1. getGlow(i) returns a function so you would have to call it like getGlow(i)(event)

  2. you didn’t specify a parameter for the function thus you would loose the event table.

  3. this would create a new function every time an event is fired which I think is not very good practice

if i have to attach a function on a listener that does not create a 

return function(event) end

does it mean that I have to called the function using wrappers?

Only if you want custom parameters in it. But you could set those on the objec itself and access them in the event function using event.target.myParam.

Usualy you don’t need a wrapper you can define like this:

local function myTouch(event)

–proccess event

end

object:addEventListener(“touch”, myTouch)

or evene

object:addEventListener(“touch”, function(event)

    --proccess event

  end;)

if the function is short and you don’t need it anywhere else

in the case above where you need a param the first coed you posted is fine or you could set a field on the object like

 local function getGlow(event) local num = event.target.myIndex if "began" == event.phase then buttonTable[num] = display.newImage(group, symbols[num].glow, symPos[num].x, symPos[num].y) elseif "moved" == event.phase then elseif "ended" == event.phase or "cancelled" == phase then buttonTable[num] = display.newImage(group, symbols[num].origin, symPos[num].x, symPos[num].y) count = count + 1 print(count) end end --load all symbols for i = 1, #symbols do buttonTable[i] = display.newImage(group, symbols[i].origin, symPos[i].x, symPos[i].y) buttonTable[i].myIndex = i buttonTable[i]:addEventListener('touch', getGlow) end end

neat. thanks :smiley: i will try to implement it

That is fine. This is called lua closures and is one of the top features of lua in my opinion.

so you don’t need to wrap it up like this?

--load all symbols for i = 1, #symbols do buttonTable[i] = display.newImage(group, symbols[i].origin, symPos[i].x, symPos[i].y) buttonTable[i]:addEventListener('touch', function() getGlow(i) end) //wrapped it up with a function instead end end

Nope @jonathan, look that get glow returns already wrappex function.

oh, okay then. cool. thanks! :smiley:

No beacuse getGlow(num) return a function so that is fine. The way you wrote it now is not actually going to work for many reasons:

  1. getGlow(i) returns a function so you would have to call it like getGlow(i)(event)

  2. you didn’t specify a parameter for the function thus you would loose the event table.

  3. this would create a new function every time an event is fired which I think is not very good practice

if i have to attach a function on a listener that does not create a 

return function(event) end

does it mean that I have to called the function using wrappers?

Only if you want custom parameters in it. But you could set those on the objec itself and access them in the event function using event.target.myParam.

Usualy you don’t need a wrapper you can define like this:

local function myTouch(event)

–proccess event

end

object:addEventListener(“touch”, myTouch)

or evene

object:addEventListener(“touch”, function(event)

    --proccess event

  end;)

if the function is short and you don’t need it anywhere else

in the case above where you need a param the first coed you posted is fine or you could set a field on the object like

 local function getGlow(event) local num = event.target.myIndex if "began" == event.phase then buttonTable[num] = display.newImage(group, symbols[num].glow, symPos[num].x, symPos[num].y) elseif "moved" == event.phase then elseif "ended" == event.phase or "cancelled" == phase then buttonTable[num] = display.newImage(group, symbols[num].origin, symPos[num].x, symPos[num].y) count = count + 1 print(count) end end --load all symbols for i = 1, #symbols do buttonTable[i] = display.newImage(group, symbols[i].origin, symPos[i].x, symPos[i].y) buttonTable[i].myIndex = i buttonTable[i]:addEventListener('touch', getGlow) end end

neat. thanks :smiley: i will try to implement it