Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

Object oriented lua classes leaking

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, it's vastly more difficult to try to figure out what's going on from your little snippets that if you just posted a complete working example demonstrating the problem.

self.components contains all the components of the window

This is probably your problem right here. Again, this is a guess, because you didn't show the create method, but if your constructor is not initializing a clickCallback member for each instance, then it's going to use the table in the class itself.

Here's an example illustrating the problem:

component = {}
component.__index = component

component.clickCallback = {}

function component.create()
  return setmetatable({}, component)
end

function component:addClickHandler(handler)
  table.insert(self.clickCallback, handler)
end

function component:click(x,y)
  for _,callback in pairs(self.clickCallback) do
      callback(x,y)
  end
end

a = component.create()
b = component.create()

a:addClickHandler(function(x,y) print("a", x, y) end)
b:addClickHandler(function(x,y) print("b", x, y) end)

a:click(10,20)
b:click(11,22)

Here's the output, which shows the symptom you described:

a       10      20
b       10      20
a       11      22
b       11      22

In other words, calling a:click calls the handler for a and b, because the clickCallback table is in the class itself, being shared by all instances of that class. The fix it make sure each instance has it's own handler table:

component = {}
component.__index = component

function component.create()
  return setmetatable({ clickCallback = {}}, component)
end

function component:addClickHandler(handler)
  table.insert(self.clickCallback, handler)
end

function component:click(x,y)
  for _,callback in pairs(self.clickCallback) do
      callback(x,y)
  end
end

a = component.create()
b = component.create()

a:addClickHandler(function(x,y) print("a", x, y) end)
b:addClickHandler(function(x,y) print("b", x, y) end)

a:click(10,20)
b:click(11,22)

Output:

a       10      20
b       11      22

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...