Class -trouble adding eventListener inside for loop

I have an issue inside one of my classes
The problem is when I try to add an event listener to an object inside a for loop

Here´s the code (stripped down) in Hud.lua:
[LUA]
local _M = {}
_M.filePath= “assets/hud/”
_M.prototype = { hudPower = display.newImage(_M.filePath … “powerButton.png”) }
_M.mt = { __index = _M.prototype }
setmetatable( _M, _M.mt )
function _M:new()
local o = {}
o.tableForListeners ={ [1]={self.hudPower, self.onTrottle} }

function o.startListeners()
for k, v in pairs (o.tableForListeners) do
v[1]:addEventListener( “touch”, v[2] )
end
end
[/LUA]

The [1]= is not valid on this line:

o.tableForListeners ={ [1]={self.hudPower, self.onTrottle} }

Remove the [1]= so that it reads:

o.tableForListeners ={ {self.hudPower, self.onTrottle} }

By not giving the inner table a named index you are saying that it is at index 1. To give it a named index you would do:

o.tableForListeners ={ aNamedIndex={self.hudPower, self.onTrottle} }

What error/problem are you getting?

Than you so much horacebury, that is correct!!

but I had to move the function o.onTrottle before the o.tableForListeners and I could not use self on the value that represent the function it will call, I had to do: o.onTrottle and not self.onTrottle

So:

  1. the function: o.onTrottle()

[LUA]

function o.onTrottle()
   print(“Trottle function”)
  end

[/LUA]

  1. The table that holds the values for both obj and function for the event listener: o.tableForListeners:

[LUA]

o.tableForListeners = {
          {self.hudTrottle, o.onTrottle}
         }

[/LUA]

  1. The generating of the event listener inside the function: o.startListener:

[LUA]

function o.startListeners()
   self.hudPanelOff.isVisible = false
   for k,v in pairs (o.tableForListeners) do
    v[1]:addEventListener( “touch”, v[2] )
   end
  end

[/LUA]

Now it all works

Now I start to wonder WHY could´t I use self on the last value?? in:

[LUA] {self.hudTrottle, o.onTrottle}[/LUA]

My own guess is this:

self.hudTrottle is referring to the variable declared in the prototype of the metatable (outside the :new() constructor so it is legit for self to reference to and the onTrottle is declared inside the :new()´s constructor and NOT in the root of the module ( _M={} ) 
…thats why I have to refer to it as o and not as self

…any comments regarding this is appreciated !! :slight_smile:

I don’t know. TBH your code is very hard to read because you’ve got lots of __ variables and you use the metatable. I’ve never really understand that and never needed to. Is it required when implementing classes in Lua? Something I don’t do either because I only use classes in C#, not Lua (partly why I like Lua.)

I would suspect that self.hudTrottle does not work because you haven’t declared .hudTrottle in your code above. If you have in your real code then I don’t know.

I have a metatable that points to a prototype (in this case). I like to think of it like this: A baby gets born ----- (the new() constructor inside the Class/module), It is born as a result of two parents with two arms and two legs and so on… ------(the prototype through the use of metatable) But as the child grows up it catches diabetes, as i did but this again makes me eat more healthy and exercise more, ------ (I maybe inherited new abilities from another superClass or changed metatable) Another thing is the difference in getting 20 kids in for dinner than to tell the kids to listen to the dinnerbell and react to it ----using the self key on the object and give it ability to listen to custom events …just some thoughts

The [1]= is not valid on this line:

o.tableForListeners ={ [1]={self.hudPower, self.onTrottle} }

Remove the [1]= so that it reads:

o.tableForListeners ={ {self.hudPower, self.onTrottle} }

By not giving the inner table a named index you are saying that it is at index 1. To give it a named index you would do:

o.tableForListeners ={ aNamedIndex={self.hudPower, self.onTrottle} }

What error/problem are you getting?

Than you so much horacebury, that is correct!!

but I had to move the function o.onTrottle before the o.tableForListeners and I could not use self on the value that represent the function it will call, I had to do: o.onTrottle and not self.onTrottle

So:

  1. the function: o.onTrottle()

[LUA]

function o.onTrottle()
   print(“Trottle function”)
  end

[/LUA]

  1. The table that holds the values for both obj and function for the event listener: o.tableForListeners:

[LUA]

o.tableForListeners = {
          {self.hudTrottle, o.onTrottle}
         }

[/LUA]

  1. The generating of the event listener inside the function: o.startListener:

[LUA]

function o.startListeners()
   self.hudPanelOff.isVisible = false
   for k,v in pairs (o.tableForListeners) do
    v[1]:addEventListener( “touch”, v[2] )
   end
  end

[/LUA]

Now it all works

Now I start to wonder WHY could´t I use self on the last value?? in:

[LUA] {self.hudTrottle, o.onTrottle}[/LUA]

My own guess is this:

self.hudTrottle is referring to the variable declared in the prototype of the metatable (outside the :new() constructor so it is legit for self to reference to and the onTrottle is declared inside the :new()´s constructor and NOT in the root of the module ( _M={} ) 
…thats why I have to refer to it as o and not as self

…any comments regarding this is appreciated !! :slight_smile:

I don’t know. TBH your code is very hard to read because you’ve got lots of __ variables and you use the metatable. I’ve never really understand that and never needed to. Is it required when implementing classes in Lua? Something I don’t do either because I only use classes in C#, not Lua (partly why I like Lua.)

I would suspect that self.hudTrottle does not work because you haven’t declared .hudTrottle in your code above. If you have in your real code then I don’t know.

I have a metatable that points to a prototype (in this case). I like to think of it like this: A baby gets born ----- (the new() constructor inside the Class/module), It is born as a result of two parents with two arms and two legs and so on… ------(the prototype through the use of metatable) But as the child grows up it catches diabetes, as i did but this again makes me eat more healthy and exercise more, ------ (I maybe inherited new abilities from another superClass or changed metatable) Another thing is the difference in getting 20 kids in for dinner than to tell the kids to listen to the dinnerbell and react to it ----using the self key on the object and give it ability to listen to custom events …just some thoughts