2010-06-25 7 views
3

나는 다양한 길이가 될 const char **을 가지고 있지만, const char **에서 루아 배열을 만들고 싶습니다.const char에서 루아 표 만들기 **

const char ** 내가 루아의 글로벌 테이블에이 배열을 변환해야이

arg[0]="Red" 
arg[1]="Purple" 
arg[2]="Yellow" 

같은 것입니다,하지만 난에 아주 좋은 아니에요으로 이것에 대해 이동하는 방법에 대한 확실하지 않다 루아 조작.

답변

3
int main() 
{ 
    char* arg[3] = { 
     "Red", 
     "Purple", 
     "Yellow" }; 

    //create lua state 
    Lua_state* L = luaL_newstate(); 

    // create the table for arg 
    lua_createtable(L,3,0); 
    int table_index = lua_gettop(L); 

    for(int i =0; i<3; ++i) 
    { 
     // get the string on Lua's stack so it can be used 
     lua_pushstring(L,arg[i]); 

     // this could be done with lua_settable, but that would require pushing the integer as well 
     // the string we just push is removed from the stack 
     // notice the index is i+1 as lua is ones based 
     lua_rawseti(L,table_index,i+1); 
    } 

    //now put that table we've been messing with into the globals 
    //lua will remove the table from the stack leaving it empty once again 
    lua_setglobal(L,"arg"); 
} 
+0

답변에 많은 감사드립니다. 이것은 매우 쉽습니다. 테이블이 지금 구조화 된 방법을 이해합니다. – Nowayz