[q2x] OT: Lua questions

Nick Trout nicktrout at shaw.ca
Sat Jan 24 01:55:08 EST 2004


I think if you want to duplicate the class you should do something like the
following, which just copies all the members.

classdef = {}
function classdef:create()
   local new = {}
   for i, v in classdef do
      new[ i ] = classdef[ i ]
   end
   return new
end

The advantage of the metamethod solution is you only have one instance of
the methods. The instance contains your member variables, but then you have
a table which contains your methods (kind of like they're static). I suppose
it's a bit like a vtable in C++. You can also then overload various
operators, e.g. +, - etc.

Using this method, if you don't find a member you could then look in a super
class if one is attached, i.e. inheritance by delegation. But since this is
a dynamic, runtime operation , if you have to do this a significant number
of times, and there is a significant hierarchy, there could be a speed
penalty. I think the author of Yindo mentioned sometime ago on lua-l that he
would avoid delegation on form a static table of methods as this should be
quicker, i.e. collect all of the methods for a particular inheritance
hierarchy in unique tables so there is only one call.

If you run a file in a state, over a previous run then you'll probably
destroy all the objects that were there, replace them and cause them to be
garbage collected. You may have problems if you have references to any of
these objects from C. It kind of depends on your set up as to what problems
might occur but generally it should just reset.


----- Original Message ----- 
From: "Brian Hook" <hook_l at pyrogon.com>
To: <q2x at icculus.org>
Sent: Friday, January 23, 2004 8:33 PM
Subject: RE: [q2x] OT: Lua questions


Okay, I sort of have Lua working kinda/sorta like how I expect it to.
Whew.  So a couple more random questions:

1.  In terms of "making things object oriented", I've seen at least
two different techniques.  One effectively copies from a global member
into a new table.

classdef = {}

function classdef:create()
   local new = {}
   for i, v in classdef do
      new[ i ] = v[ i ]
   end
   return new
end

Or something like that.  Then there's the other mechanism that
involves copying the metatable __index.  Is there a particular
advantage of one over the other?

2.  This is a bit more complicated, but I wonder what the effect of
running a script multiple times is?  Specifically, if I have a
monster.lua, I load it, run it, then later I edit it and reload it, is
there expected behaviour?

Yeah yeah, I should go join lua-l again...

Brian





More information about the q2x mailing list