私はlua fo AIを使用してプログラムを書くつもりです。 しかし、私は私のcppファイルからのluaスクリプトをロードしようとすると、私はこのエラーメッセージを持っている:ここでC++を使用したLuaスクリプト:グローバル 'io'(ゼロ値)のインデックスを作成しよう
-- toto.lua:1: attempt to index global 'io' (a nil value)
は私のluaスクリプトです:
io.write("Running ", _VERSION, "\n")
そして、ここでは私のcppファイルです:
void report_errors(lua_State *L, int status)
{
if (status!=0) {
std::cerr << "-- " << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1); // remove error message
}
}
int main(int argc, char** argv)
{
for (int n=1; n<argc; ++n) {
const char* file = argv[n];
lua_State *L = luaL_newstate();
luaopen_io(L); // provides io.*
luaopen_base(L);
luaopen_table(L);
luaopen_string(L);
luaopen_math(L);
std::cerr << "-- Loading file: " << file << std::endl;
int s = luaL_loadfile(L, file);
if (s==0) {
s = lua_pcall(L, 0, LUA_MULTRET, 0);
}
report_errors(L, s);
lua_close(L);
std::cerr << std::endl;
}
return 0;
}
ありがとうございます。
コードは、実行するファイルごとに独立したLua状態を作成します。これにより、異なるファイルが通信できなくなります。これはあなたが望むものではないかもしれません。 – lhf