lua-users home
lua-l archive

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


Hi there,
 
I have had lua integrated into my project quite successfully for a while now.  I have lots of objects each with their own states spawned off a main state and running in a sandboxed environment to avoid global variables clashes.  Some of the objects have enough shared functionality that I have started to divide code up into modules and use "require" to import the functionality back in.  However, I started to run into problems when the modules needed to store some sort of state.  Now, my understanding of how modules work is that a module is loaded into a table, stored in a "packages" table, then a copy is also stored in the globals table of the enviornment it is loaded into, and another copy returned which can be stored in a differently named variable by the code calling require if desired.  As lua doesn't work with references, if the module has a variable within it, then there will now by potentially 3 different copies of this variable.  If this module is "require"d in 2 seperate environments then you will have 5 copies, 1 in "packages", 1 in each of the environments globals table, and the locally stored versions if used.  This way you cannot share data between the 2 different environments as they will use different versions of the module.  Am I correct so far?
 
Working on this assumption I wrote my own require functionality in a module thus:
 
function Require(pack)
        require(pack)
        local t = {}
        local mt = { __index = function(t,k) return package.loaded[pack][k] end }
        setmetatable(t, mt)
        return t
end
 
The idea behind this is that we return a proxy table that always references the table in package.loaded.  If we never use the global instance, and only use the returned table then we will only ever reference the copy stored in package.loaded, thus all environments using the module will share the same one, and only 1 copy of the data is ever used.  This will only work however if there is only ever one version of "package" shared across all the environments.  Is this the case?  My system seems to work in some cases but not in others, so I'm guessing there is a hole in my logic somewhere.  Can anyone point out what I might be doing wrong?
 
Thanks in advance,
 
Tom


DISCLAIMER
This email is sent by The Creative Assembly Limited company No. 03425917, registered in England & Wales registered office 27 Great West Road, Middlesex, TW8 9BW, England. The contents of this e-mail and any attachments are confidential to the intended recipient and may also be legally privileged. Unless you are the named addressee (or authorised to receive for the addressee) of this email you may not copy, disclose or distribute it to anyone else. If you have received this email in error, please notify us immediately by e-mail on
postmaster@creative-assembly.co.uk and then delete the email and any copies. The Creative Assembly Limited have made all reasonable efforts to ensure that this e-mail and any attached documents or software are free from software viruses, but it is the recipient's responsibility to confirm this.