Re: [GD-General] Re: Scripting Systems
Brought to you by:
vexxed72
From: <cas...@ya...> - 2004-02-01 19:40:38
|
Brian Hook wrote: > It also might be possible to hack Lua so that assignments to > undeclared variables generate compile time errors, but I'm not > familiar enough with Lua innards to know if that would be easy or > difficult. You can do that without "hacking" lua. This is from the top of my head, but if you want to follow this direction I can provide a more detailed explanation, or you may also ask on the lua mailing list for more info. I think that was a frequently asked question and is probably on the FAQ. -- Catch access to undeclared properties of the given table. function CatchUndeclared( table ) local meta = getmetatable( table ) if not meta then meta = {} setmetatable( table, meta ) end meta.__newindex = function (self, key, value) error( "cannot add '"..tostring(key).."', '"..tostring(value).."' to protected table" ) end end Whis this function you can protect the given table, so that no new keys can be added to it: table = { key1 = 1 } table.key2 = 2 CatchUndeclared( table ) table.key1 = 2 table.key2 = 3 table.key3 = 4 -- this is not valid! This also applies to the global table: CatchUndeclared( _G ) table = {} newtable = {} -- this is not valid! Hope that helps. -- Ignacio Castaño cas...@ya... |