[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Uses for metatables
- From: Tim Conkling <tconkling@...>
- Date: Fri, 15 Aug 2003 03:32:44 -0400
As I described in a previous message, entities in my game are controlled by
Lua scripts. An entity script should contain only a table populated with
functions that can be called on the entity as well as entity-specific
variables that can be modified within the functions. Since more than one
entity can use a particular script, each entity gets its own copy of its
corresponding Lua table (all that entity's member variables and functions).
Each entity function is passed (as its only parameter) this table copy so
that it can modify characteristics of the entity without affecting all other
entities using that particular script.
A script file for an entity named "MySprite" might look like this:
MySprite = {}
MySprite.speed = 0
MySprite.update = function(self) self.speed = self.speed + 1 end
I want to guard against the following situation:
MySprite.update = function(self) MySprite.speed = MySprite.speed + 1 end
where the author of the script writes a function that accidentally modifies
the original table rather than the entity's own copy.
Are metatables appropriate in this situation? I was thinking of creating a
generic "table-protecting" metatable that raises errors for index, newindex,
and call events, and then installing this as the metatable for each original
entity table that is loaded. Can I create one table for this purpose and use
it as the metatable for each table that I need to protect, or do I need to
make copies of this table? Also, is there a better way to protect against
this sort of behavior in scripts?
Thanks,
Tim
--
Tim Conkling
tconkling@brown.edu