[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  General
. . -> [Subject]  Lua: Field/Variable Change Event
Home  |  Users  |  Search  |  FAQ
Register forum user name
Forgotten password?

Lua: Field/Variable Change Event

It is now over 60 days since the last post. This thread is closed.   [New subject]  Start a new subject   [Refresh] Refresh page

Posted by LJY   (3 posts)  [Biography] bio
Date Sat 22 Jan 2011 12:15 AM (UTC)  quote  ]
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?
[Go to top] top

Posted by Nick Gammon   Australia  (19,609 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Sat 22 Jan 2011 02:40 AM (UTC)  quote  ]
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":


- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by LJY   (3 posts)  [Biography] bio
Date Reply #2 on Sat 22 Jan 2011 02:59 AM (UTC)  quote  ]

Amended on Sat 22 Jan 2011 03:00 AM (UTC) by LJY

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.
[Go to top] top

Posted by LJY   (3 posts)  [Biography] bio
Date Reply #3 on Sat 22 Jan 2011 05:15 AM (UTC)  quote  ]

Amended on Sat 22 Jan 2011 05:23 AM (UTC) by LJY

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]

__newindex = function (t,k,v)
_G[k] = v
-- Whatever things you need to do when the value changes.
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.
[Go to top] 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.


It is now over 60 days since the last post. This thread is closed.   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum

[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.


Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Web site powered by FutureQuest.Net]