Register forum user name Search FAQ

Gammon Forum

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.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Serialized tables and metamethods

Serialized tables and metamethods

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by tobiassjosten   Sweden  (79 posts)  Bio
Date Fri 07 Oct 2005 11:32 AM (UTC)
Message
Using Nick's serializer in http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=4960 I've been able to save some tables into MUSHclient variables, which is all good and dandy. However, now I want to stick some metamethods on those tables and it just wont work. I haven't got much experience with metatables, besides getting it to work, so I figure there's something I'm missing here.

Currently I have this variable (skoid_cfg) saved with the serializer.
cfg = {}
  cfg["cure_salve"] = true
  cfg["heal_mana"] = true
  cfg["heal_mana_offset"] = 15
  cfg.loot = true
  cfg.timer = true


The "skoid_cfg" variable is loaded with the following code.
if GetVariable("skoid_cfg") then loadstring(GetVariable("skoid_cfg"))() end


Now I want to attach two metamethods to this table. They look like this.
skoid_mt =  { __index = function (t, name) gui_update() end; __newindex = function (t, name, val) gui_update end; }
setmetatable(cfg, skoid_mt)


But lo and behold! No matter where I put the "setmetatable", it wont work.. I tried defining the cfg table before loading it, and setting the metatable right after. But I figure it's all overwritten when loading it (since it's redefined). But since setting the metatable after loading the table wont work neither, I'm out of ideas..

Anyone?

Simplicity is Divine | http://nogfx.org/
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #1 on Sat 08 Oct 2005 12:19 AM (UTC)

Amended on Sat 08 Oct 2005 01:33 AM (UTC) by Nick Gammon

Message
It worked for me, I tried this in the immediate window:



cfg = {}
  cfg["cure_salve"] = true
  cfg["heal_mana"] = true
  cfg["heal_mana_offset"] = 15
  cfg.loot = true
  cfg.timer = true



skoid_mt =  
  { __index = function (t, name) 
    Note ("__index called for " .. name);
   end; 
   __newindex = function (t, name, val) 
   Note ("__newindex  called for " .. name .. ", value " .. val);
   end; 
  }

setmetatable(cfg, skoid_mt)

Note (cfg.abcd)
cfg.blah = 22



Output


__index called for abcd
nil
__newindex  called for blah, value 22


You know the metamethods are only called for an indexed item that does not exist? If you do something like:


cfg.loot = false


That will not call the metamethod. If you are trying to make it refresh something on access (which you seem to be doing) then you need to make an empty proxy table. This is explained in the Lua book on page 115.

See this for explanation (the online version):


http://www.lua.org/pil/13.4.4.html


The method described there maintains an empty table, and thus every access calls the metamethod. Of course you want the data to be stored/retrieved, so the metamethod does it via the local table.



- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by tobiassjosten   Sweden  (79 posts)  Bio
Date Reply #2 on Sat 08 Oct 2005 01:27 AM (UTC)
Message
That explains alot. Could have sworn my tests showed me that the metamethods were called even when changed allready existing keys.. Proxy tables looks neat, I'll have to do some experimenting with them.

Thanks.

Simplicity is Divine | http://nogfx.org/
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.


13,487 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.