Return parameters from a listener

Hi folks - I’m trying to eliminate some global variables in my code and I keep bogging down on a couple issues.

I have 2 local listeners in my program, a touch listener and a collision listener. From the collision listener, I would like to return a table called collideTable so that I can use the values in that table in other functions. Right now, that variable is global but I want to figure out how to make it local and still access it’s values in other functions.

[lua]local function collision( self, event ) – Self is the hole and other is the peg. Event is the collision
local pegColor = event.other.myName – assign name of the peg (either Red Peg or Blue Peg)
local whichPeg = event.other.value – which peg is being moved
local holeColor = self.myName --assign name of hole (either Red Peg Holes or Blue Peg Holes)
local holeState = self.state – is the hole empty or filled
local holeValue = self.value – this is the value of the hole
local holeNumber = self.number – this is the hole number (starter holes are 1 and 2)
–print (pegColor…" and "…holeColor)

– increase peg’s hit count so it will be able to go into a hole
if pegColor == “Red Peg” then
redPeg[whichPeg].hitCount = redPeg[whichPeg].hitCount + 1
elseif pegColor == “Blue Peg” then
bluePeg[whichPeg].hitCount = bluePeg[whichPeg].hitCount + 1
end

– check if the peg is trying to go into the correct colored hole.
if (pegColor == “Red Peg” and holeColor == “Red Peg Holes”) or (pegColor == “Blue Peg” and holeColor == “Blue Peg Holes”) then
rightHole = true
else rightHole = false
end

if event.phase == “began” and rightHole == true and holeState then --only return true if collision has begun, the peg is colliding with the right colored hole and hole is empty
collideTable = { true, holeNumber, holeValue, whichPeg, pegColor }
else
collideTable = { false, holeNumber, holeValue, whichPeg, pegColor }

end
end[/lua] [import]uid: 25787 topic_id: 6958 reply_id: 306958[/import]

I mean to also include the code for the collision listeners. Right now the collideTable variable is global as are the tables bluePegHoles and redPegHoles. I need to make them local because the memory usage is really messy.

[lua]bluePegHoles[p].collision = collision
bluePegHoles[p]:addEventListener( “collision”, bluePegHoles[p]) – add collision table listener to each hole sensor

redPegHoles[p].collision = collision
redPegHoles[p]:addEventListener( “collision”, redPegHoles[p]) – add collision table listener to each hole sensor[/lua]

[import]uid: 25787 topic_id: 6958 reply_id: 24357[/import]