lua-users home
lua-l archive

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

Hey everyone, (and thanks very much to all of you for the help!)

So, it seems like there a couple of equivalent options. One is using
setfenv, and then linking in the global stuff I need (as in Enrico's
solution below or Fabio's code)..

Another is maybe to put make my level script files modules - i.e. put
module("level1", package.seeall) at the top of level1.lua and then require
"level1" after that level is loaded..

But it seems like, either way, I would then have to access all the
functions/objects in my level code with level1.a().  This is problematic,
because I already have a load of places in xml files (which describe the
level graphically and structurally) where I am just calling a();  .. which I
don't want to change..

An ideal solution would be a module-like structure, but where all the
functions in my level remain global (accessible from the outside without
doing level.a() ) ..

Also, as an aside, I'm loading my scripts in C, with
luaL_loadbuffer+lua_pcall or luaL_dostring. It seems that these functions
return only an error int - is there no C equivalent for the lua code:
proc,err = loadstring(script) ? 



-----Original Message-----
[] On Behalf Of Enrico Colombini
Sent: 24 March 2010 13:08
To: Lua list
Subject: Re: Cleaning up specific lua scripts

Ricky Haggett wrote:
> How would I go about loading the level script into a table?

Here is a simple example. This is your level:

-- this is "level1.lua"

function a()

function b()
     print("Ships: " .. ships)

ships = 5


and this is the level loader:

-- this is "loader.lua"

level = {} -- the table containing a level

-- read "lev1.lua" into level
local t = assert(loadfile("lev1.lua")) -- could use loadstring
setfenv(t, level)
local r, err = pcall(t) -- execute loaded chunk to define stuff
r = r or error('error reading level')

-- add global functions to level (just an example)
level.print = print

-- call function a() from outside the level table


> Also, if I used setfenv to change the environment, aren't I just moving
> problem? If I then have to add global functions to this same table, how
> I differentiate them from the level functions when I need to clean up?

No need to differentiate, just throw away the level table:

   level = nil

If you must access many global values/functions, a trick such as Steve 
suggested would be better than copying them to 'level'. The copy, on the 
other hand, allows fine control over global accesses (there are other, 
possibly better, intermediate solutions).

> I am thinking maybe my solution is going to be (shallow) copying _G before
> loading a level, then comparing the copy with _G after the level load. Any
> objects which don't exist in the copy, but do exist in _G should be the
> level-specific ones

Too much work, I prefer lazy solutions :-)