lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



Tim Conkling wrote:
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?

Hi Tim,

index and newindex events cannot save you from the MySprite.speed versus self.speed error unless you've set MySprite to nil.

Creating one metatable is fine.  Many tables can share the same metatable.

You might consider starting your script with:

  local MySprite = {}

and then end it with:

  return MySprite

If you load the script with luaL_loadfile, you'll get a function that will return a new anonymous 'MySprite' table each time it is called. This way there is no need for copying, and no need for metatables, and your problem is moot.

- Peter