2016-09-01 5 views
3

C++を使用してLuaに既に存在するグローバルテーブルにアクセスするにはどうすればよいですか? 以下は私が試したコードです。私は、グローバル変数を作成し、Luaの中でローカルにそのローカルを変更しようとしたが、物事はC++でLuaグローバルテーブルにアクセスする

problem = 
{ 
    {9, 0, 0, 1, 0, 0, 0, 0, 5}, 
    {0, 0, 5, 0, 9, 0, 2, 0, 1}, 
    {8, 0, 0, 0, 4, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 8, 0, 0, 0, 0}, 
    {0, 0, 0, 7, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 2, 6, 0, 0, 9}, 
    {2, 0, 0, 3, 0, 0, 0, 0, 6}, 
    {0, 0, 0, 2, 0, 0, 9, 0, 0}, 
    {0, 0, 1, 9, 0, 4, 5, 7, 0}, 
} 

アップデート1

int main() 
{ 
    lua_State *lua_state = luaL_newstate(); 
    luaL_openlibs(lua_state); 
    luaL_loadfile(lua_state, "main.lua"); 

    lua_getglobal(lua_state, "problem"); 

    //lua_pushglobaltable(lua_state);  // Get global table 
    lua_pushnil(lua_state);    // put a nil key on stack 
    while (lua_next(lua_state, -2) != 0) { // key(-1) is replaced by the next key(-1) in table(-2) 
     std::string name = lua_tostring(lua_state, -2); // Get key(-2) name 
     std::cout << name << std::endl; 
     lua_pop(lua_state, 1);    // remove value(-1), now key on top at(-1) 
    } 
    lua_pop(lua_state, 1);     // remove global table(-1) 

    lua_call(lua_state, 0, 0); 

    return 0; 
} 

Luaの

 lua_State *lua_state = luaL_newstate(); 
     luaL_openlibs(lua_state); 
     // lua_createtable(lua_state, 0, 81); 
     // for (int i = 1; i <= 81; i++) 
     // { 
     // lua_pushnumber(lua_state, i); 
     // lua_pushnumber(lua_state, grid_[i - 1]); 
     // lua_settable(lua_state, -3); 
     // } 
     // 
     // lua_setglobal(lua_state, "arg"); 

     // lua_createtable(lua_state, 81, 1); 
     // 
     // for (int i = 1; i <= 81; i++) 
     // { 
     // lua_pushnumber(lua_state, i); 
     // lua_pushnumber(lua_state, grid_[i - 1]); 
     // lua_settable(lua_state, -3); 
     // } 
     // lua_setglobal(lua_state, "arg"); 


     luaL_loadfile(lua_state, "main.lua"); 
     lua_call(lua_state, 0, 0); 

     int t = 2; 

     /* table is in the stack at index 't' */ 
     lua_pushnil(lua_state); /* first key */ 
     while (lua_next(lua_state, t) != 0) { 
      /* uses 'key' (at index -2) and 'value' (at index -1) */ 
      printf("%s - %s\n", 
       lua_typename(lua_state, lua_type(lua_state, -2)), 
       lua_typename(lua_state, lua_type(lua_state, -1))); 
      /* removes 'value'; keeps 'key' for next iteration */ 
      lua_pop(lua_state, 1); 
     } 

のLuaを働くように見えるいけないみました

problem = 
{ 
    {9, 0, 0, 1, 0, 0, 0, 0, 5}, 
    {0, 0, 5, 0, 9, 0, 2, 0, 1}, 
    {8, 0, 0, 0, 4, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 8, 0, 0, 0, 0}, 
    {0, 0, 0, 7, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 2, 6, 0, 0, 9}, 
    {2, 0, 0, 3, 0, 0, 0, 0, 6}, 
    {0, 0, 0, 2, 0, 0, 9, 0, 0}, 
    {0, 0, 1, 9, 0, 4, 5, 7, 0}, 
} 

print("Lua Works") 
user_input = io.read(); 
+0

グローバル変数にアクセスするには、 'lua_getglobal(L、" problem ");'を実行します。しかし、あなたのコードにはより多くの問題があります。あなたのテーブルは配列の配列であり、コードはキーと値を構成する単純なテーブルを反復しようとします。参照のためにこの他の質問をチェックしてください。http://stackoverflow.com/questions/29287988/iterating-over-table-of-tables-with-the-lua-c-api –

答えて

0

あなたはLuaスタックを反復するための任意の値。
そのint t=2;は何も反映されておらず、スクリプトはスタックに残す値を返しません。
グローバルテーブルにアクセスする例については、PIL book: 25.1 – Table Manipulationを参照してください。

+0

'問題'は変数ではありませんか? – JekasG

+0

Luaグローバルテーブルに格納されていますが、lua_getglobal()を呼び出すか、 'return'ステートメントでスクリプトから返すまで、Luaスタックにはありません。 – Vlad

+0

C++でどのように反復したり、そのテーブルのコピーを持っていますか? – JekasG

関連する問題