I've read quite a bit about object oriented lua (setting the metatable), and i have constructed a system, complete with inheritance.
My problem is now, that some of the variables seem to be leaking into each other. if i call a function called window:click(x, y)
the function calls just fine. the job of this function is the notify all my components of a click. which it is doing
for number, component in pairs(self.components) do
component.focus = false
component:click(x, y, msg)
end
self.components
contains all the components of the window
To act as a baseclass for all the components i have a class called component.lua, this file creates a table called components, and adds a create()
method to it (that does all the usual OO lua stuff), this baseclass contains all the methods and variables i want in all my components, including component:click(x, y)
and once again, it get's called.
for key, callback in pairs(self.clickCallback) do
callback()
end
return
the clickCallback
table contains functions that should be called when the component is notified. and is initialized inside component.lua
From here i inherit this class over to my other classes, simply by setting my metatable of my new component (textbox, button, label etc.). These components are what get's added to the self.components
table in the window.
The problem is that each of these components should have their own clickCallback table. Which i am writing to via a setter in component.lua
function component:addClickHandler(handler)
table.insert(self.clickCallback, handler)
end
but when i call click(x,y)
on one component it calls all the clickHandlers, whether that be for another button or a label.
As you could see above, i am setting a parameter called focus
this seems to experience the same problem, where setting it for one component (as you can see i am looping through each of them) sets it for all (so if i have 4 components the focus gets reset 4 times on each component)
Why is lua doing this, and what can be done to fix it?
See Question&Answers more detail:
os