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

  • SEARCH
  • Home
  • 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 6871547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:49:10+00:00 2026-05-27T03:49:10+00:00

Does lua have an event handler build in or does it have a lib

  • 0

Does lua have an “event handler” build in or does it have a lib available to do that?

So that, an example, when “a = 100” an event happens.

Something else rather than using:

while true do
 if a == 100 then
   [...]
   break;
 end
end

Or simply adding a sleep to it. The “while true do” is just an example but its a terrible one.

  • 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-05-27T03:49:11+00:00Added an answer on May 27, 2026 at 3:49 am

    Lua operates in a single-thread, so any checking must be explicitly performed by your code.

    The act of performing code immediately upon a variable changing is known as “watching”.

    If you are programming in an environment where a set of code is run every frame (such as a game), you can check manually.
    For example:

    WatchedVariables = {
        a = 5,
        b = 22,
    }
    WatchedVariables_cache = {}
    for k,v in pairs(WatchedVariables) do
        WatchedVariables_cache[k] = v
    end
    
    function OnFrame()
        print("NEXT FRAME! (possibly 1 second later or something)")
        for k,v in pairs(WatchedVariables) do
            local v_old = WatchedVariables_cache[k]
            if v ~= v_old then
                -- this is the "callback"
                print(tostring(k).." changed from "..tostring(v_old).." to "..tostring(v))
    
                WatchedVariables_cache[k] = v
            end
         end
     end
    
     function SomeFunctionThatOperatesSomeTime()
         print("about to change a, brother!")
         WatchedVariables.a = -7
         print("a is changed")
     end
    

    Upon the next frame, the callback code (the print) will be executed.
    The disadvantage of this approach is that the callback code is not printed immediately after WatchedVariables.a is set to -7, that is: the output will be:

    about to change a, brother!
    a is changed
    NEXT FRAME! (possibly 1 second later or something)
    a changed from 5 to -7
    

    To prevent this potentially undesired behaviour, a setter function can be used, for example:

    MyObject = {
        _private_a = 5,
        set_a = function(self, new_value_of_a)
            self._private_a = 5
            -- callback code
            print("a set to "..tostring(new_value_of_a))
        end,
        get_a = function(self)
            return self._private_a
        end
    }
    
    function SomeFunctionThatOperatesSomeTime()
         print("about to change a, brother!")
         MyObject:set_a(-7)
         print("a is changed")
     end
    

    The output of this code shows that the callback runs immediately:

    about to change a, brother!
    a set to -7
    a is changed
    

    To make this more comfortable, Lua provides metatables that make such behaviour transparent to the programmer.
    Example:

    MyObject = {
        __privates = {
            a = 5,
        }
    }
    MyObject_meta = {
        __index = function(self, k)
            return rawget(self, "__privates")[k]
        end,
        __newindex = function(self, k, v)
            rawget(self, "__privates")[k] = v
            -- callback code
            print("a set to "..tostring(v))
        end,
    }
    setmetatable(MyObject, MyObject_meta)
    
    function SomeFunctionThatOperatesSomeTime()
         print("about to change a, brother!")
         MyObject.a = -7
         print("a is changed")
     end
    

    The output of this code will be the same as the previous example:

    about to change a, brother!
    a set to -7
    a is changed
    

    Here’s an implementation for your example case:

    MyObject = {
        __privates = {
            a = 5,
        }
        __private_callback = function(self, k, ov, v)
            if k == "a" and v == "100" then
                print("a is 100!")
            end
        end
    }
    MyObject_meta = {
        __index = function(self, k)
            return rawget(self, "__privates")[k]
        end,
        __newindex = function(self, k, v)
            local privates = rawget(self, "__privates")
            local ov = privates[k]
            privates[k] = v
            rawget(self, "__private_callback")(self, k, ov, v)
        end,
    }
    setmetatable(MyObject, MyObject_meta)
    
    function SomeFunctionThatOperatesSomeTime()
         MyObject.a = -7 -- prints nothing
         MyObject.a = 100 -- prints "a is 100!"
         MyObject.a = 22 -- prints nothing
     end
    

    Why are the variables __privates and __private_callback prefixed with two underscores? It is convention to prefix private members that should not be accessed in typical programming situations with two underscores. If you familiar with object-oriented methodology and it’s implementation in languages such as Java and C++, you will understand how it is similar to the private and protected keywords.

    If you are familiar with the C# language, you may see how the set_a/get_a and metatable implementations are similar to accessors (set/get).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does Lua support something like C's __LINE__ macro, which returns the number of the
Does anyone have any recommendations of tools that can be of assistance with moving
Does anyone use have a good regex library that they like to use? Most
Does Lua have a builtin sum() function? I can't seem to find one, and
I have several Lua scripts that run experiences and output a lot of information,
I have a class that is wrapped with swig, and registered with lua. I
I have programmed a plugin in Lua for a game that sends player information
I'm using a graphics library that lets you program in Lua. I have a
I have a small app that uses Lua linked as dll (not static). I
Does Lua provide a function to make the first character in a word uppercase

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.