Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8242665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:14:44+00:00 2026-06-07T21:14:44+00:00

I’ve read quite a bit about object oriented lua (setting the metatable), and i

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:14:45+00:00Added an answer on June 7, 2026 at 9:14 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.