LuaJ从两个不同的LuaScript中加载两个具有相同名称的函数

LuaJ从两个不同的LuaScript中加载两个具有相同名称的函数

问题描述:

我有两个Lua脚本,其中包含具有相同名称的函数:

I have two Lua Scripts containing functions with the same name:

luaScriptA:

luaScriptA:

function init() 
print( 'This function was run from Script A' )
end

luaScriptB:

luaScriptB:

function init() 
print( 'This function was run from Script B' )
end

我想使用LuaJ将这两个函数都加载到globals环境中,对于一个脚本,我通常按以下步骤进行操作:

I would like to load both these functions using LuaJ into the globals environnment, for one script I usually do it as follows:

LuaValue chunk = globals.load(new FileInputStream(luaScriptA), scriptName, "t",
globals);
chunk.call();

这会将函数init()加载到全局变量中,我可以使用以下命令从Java执行该函数:

This will load the function init() into globals and I can execute this function from java with:

globals.get("init").call();

问题是在我加载第二个脚本时出现的,这将覆盖以前声明的相同名称的所有函数.有什么办法可以防止这种情况并轻松区分这两种功能?例如:

The problem comes in when I load a second script, this will overwrite all functions with the same name previously declared. Is there any way I can prevent this and easily distinguish between the two functions? For example something like:

globals.get("luaScriptA").get("init").call(); //Access the init function of script A
globals.get("luaScriptB").get("init").call(); //Access the init function of script B

请注意,该脚本还包含其他功能,我的目标是在脚本中运行单个功能,而不是一次运行完整的脚本.在JME平台上工作.

Please note that the script contains other functions as well and my goal is to run individual functions within the script, not the complete script at once.Working on the JME platform.

将函数放在表中

luaScriptA:

luaScriptA:

A = {} -- "module"
function A.init() 
    print( 'This function was run from Script A' )
end

luaScriptB:

luaScriptB:

B = {} -- "module"
function B.init() 
    print( 'This function was run from Script B' )
end

那你就要做

globals.get("A").get("init").call();
globals.get("B").get("init").call();