lua-users home
lua-l archive

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


You can use something like that (tested) :
 
---- foo.lua ----
-- declare helper functions
function push_env_table(t)
 local g = getfenv(0)
    setmetatable(t, {__index=g})
 setfenv(0, t)
end
 
function pop_env_table()
 local t = getfenv(0)
    local g = getmetatable(t).__index
 setfenv(0, g)
end
 
-- declare local stuff
foo = "foo"
function ffoo()
 return "ffoo"
end
 
-- loading a file require 4 lines, you could make a function of it
envbar = {}
push_env_table(envbar)
require("bar")
pop_env_table()
 
-- to use a file simply prepend the env table you defined
print(foo, envbar.foo)
print(ffoo(), envbar.ffoo())
--------------------
 
---- bar.lua ----
foo = "bar"
function ffoo()
 return "fbar"
end
--------------------


De : lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] De la part de Raymond Jacobs
Envoyé : 5 mai 2006 09:52
À : Lua list
Objet : multiple lua files with same-named functions

Greetings All,
 
I am having a bit of an issue with the system I've made.
 
I have a single lua_State with a number of registered C functions in it.
 
whenever I want to execute another script file, I make a new co-routine (from C),
I load the lua code into this new co-routine, and then call a function on it.
 
the issue is that all of these different script files have functions of the same name,
and it seems that if i have previously loaded a script with the function 'onCreate' in it,
and I load a new script which doesn't have an onCreate function (yet it tries to call it) it will find
the previously loaded onCreate function, I would rather that it say onCreate doesn't exist (since it is not in the script I've loaded),
but it seems whatever code i load all of the functions get stored into the global state, is this right?
 
and if so how can I solve it? a brute force idea is to clear all the functions from the global state before loading a new script
but that sounds awful =/
 
any info is appreciated, thanks!
 
Raymond Jacobs
Owner,
Ethereal Darkness Interactive
www.edigames.com