Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ Programming
➜ General
➜ Lua: Field/Variable Change Event
|
Lua: Field/Variable Change Event
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| LJY
(3 posts) Bio
|
| Date
| Sat 22 Jan 2011 12:15 AM (UTC) |
| Message
| | 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? | | Top |
|
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #1 on Sat 22 Jan 2011 02:40 AM (UTC) |
| Message
| I don't think many languages support detecting variable changes per se.
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 |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| LJY
(3 posts) Bio
|
| Date
| Reply #2 on Sat 22 Jan 2011 02:59 AM (UTC) Amended on Sat 22 Jan 2011 03:00 AM (UTC) by LJY
|
| Message
| 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. | | Top |
|
| Posted by
| LJY
(3 posts) Bio
|
| Date
| Reply #3 on Sat 22 Jan 2011 05:15 AM (UTC) Amended on Sat 22 Jan 2011 05:23 AM (UTC) by LJY
|
| Message
| 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. | | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
17,688 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top