Re: Re: Scripting Systems

Ignacio Castaño <[email protected]>
Newsgroups gmane.games.devel.general
Message-ID <[email protected]>
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
[email protected]


-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
_______________________________________________
Gamedevlists-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gamedevlists-general
Archives:
http://sourceforge.net/mailarchive/forum.php?forum_id=557
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.