私はluaをc++と統合しようとしている小さなプロジェクトに取り組んでいます。しかし、私の問題は次のとおりです。
複数のluaスクリプトがあります。それらをs1.luas2.luaおよびs3.luaと呼びましょう。これらのそれぞれには、setVars()およびexecuteResults()という関数があります。
これで、LuaL_dofileを介して、setVars()やexecuteResults()を使用した直後にluaファイルを呼び出すことができます。ただし、ここでの問題は、s2.luaをロードした後、s1.luaの関数を呼び出すことができなくなることです。これは、関数へのアクセスを回復するためにs1.luaでLuaL_dofileをやり直す必要があることを意味します。そうすることで、s2.luaの関数にアクセスできなくなります。
すべてのluaファイルを連続してロードし、その後、それらの関数を自由に呼び出し始める方法はありますか?s1-> executeResults()s5-> executeResults()s3-> setVars()などのようなもの。
私は現在、boost :: filesystemを使用してフォルダー内のすべてのluaファイルを検出するシステムをすでに導入しています。次に、これらのファイル名をベクターに保存し、ベクターを繰り返し処理して、各luaファイルを連続してロードします。
ベクトルをluaファイル名で埋める前に、プラグインのロード関数は現時点では次のようになっています。
void Lua_plugin::load_Plugins(){
std::vector<std::string>::const_iterator it;
for (it=Lua_PluginList.begin(); it!=Lua_PluginList.end(); it++){
std::cout<<"File loading: " << *it << std::endl;
std::string filename = *it;
std::string filepath = scriptdir+filename;
if (luaL_loadfile(L, filepath.c_str()) || lua_pcall(L, 0, 0, 0)) {
std::cout << "ScriptEngine: error loading script. Error returned was: " << lua_tostring(L, -1) << std::endl;
}
}
}
もう少し明確にするために、.luaにあるのは次のようなものだけです。
-- s1.lua
setVars()
--do stuff
end
executeResults()
--dostuff
end
などですが、両方を続けてロードした後、s1.luaのsetVars()とs2.luaのsetVars()を呼び出せるようにしたいと思います。