Hi, I have just switched to Lua on my mushclient and I am starting to search for how to detect a change in value in a variable or table field. I tried googling for it but there hasn't been fruitful. Is it simply because there is no way to do this in Lua?
Lua: Field/Variable Change Event
Posted by LJY on Sat 22 Jan 2011 12:15 AM — 4 posts, 18,398 views.
I don't think many languages support detecting variable changes per se.
Do you mean something like this?
You can do this to a certain extent in Lua by using metatables and keeping a "proxy" table, and using the __newindex function call to detect attempts to add a variable.
This is covered here in the Programming in Lua book under the section "Tracking Table Accesses":
http://www.lua.org/pil/13.4.4.html
Do you mean something like this?
foo = 2 --> call some function to say foo now has the value 2
You can do this to a certain extent in Lua by using metatables and keeping a "proxy" table, and using the __newindex function call to detect attempts to add a variable.
This is covered here in the Programming in Lua book under the section "Tracking Table Accesses":
http://www.lua.org/pil/13.4.4.html
Yup, I want to be able to detect a change in a variable's value and make that call a particular function to handle it. I was using Jscript for mushclient before that and it seemed that Jscript is capable of doing that (at least I found examples when I googled for it, but I had not moved it to my script file to see if it only works on browsers).
I switched to Lua because I see Lua providing more features, with some exclusive to Lua.
I switched to Lua because I see Lua providing more features, with some exclusive to Lua.
Ok thanks, I found a way to do this, it may not look really professional, but at least it is working for me. It is modified from the examples in the Lua book 13.4.4, aimed to not make every table I have to have a metatable like this. I'm posting it here as it might be useful for some people here.
DetectChange = {}
DetectChange.mt = {
__index = function (t,k)
return _G[k]
end,
__newindex = function (t,k,v)
_G[k] = v
-- Whatever things you need to do when the value changes.
end,
}
setmetatable(DetectChange, DetectChange.mt)
-- Usage Example: DetectChange.test = 5
-- This will change the value of test variable to 5.
-- At the same time, it will trigger certain action that you have defined above.
-- Tested to work in layered tables, as long the global environment _G points to the right global table.
-- You need to change a variable value in this format, it does not work when you change the variable directly.
DetectChange = {}
DetectChange.mt = {
__index = function (t,k)
return _G[k]
end,
__newindex = function (t,k,v)
_G[k] = v
-- Whatever things you need to do when the value changes.
end,
}
setmetatable(DetectChange, DetectChange.mt)
-- Usage Example: DetectChange.test = 5
-- This will change the value of test variable to 5.
-- At the same time, it will trigger certain action that you have defined above.
-- Tested to work in layered tables, as long the global environment _G points to the right global table.
-- You need to change a variable value in this format, it does not work when you change the variable directly.