2012-07-13 3 views
1

안녕하세요. 저는이 겉으로보기에는 단순한 작업으로 정말 어려움을 겪고 있습니다. C의 함수에 전달 된 테이블의 속성에 액세스 할 수 있지만 그 안에 만든 하위 테이블의 멤버는 액세스 할 수 없습니다.C 언어의 루아에서 다차원 테이블에 액세스하는 방법?

기본적으로 속성 테이블에서 문자열을 추출 할 수 있으므로 사용자의 기대에 따라 "휠"을 만들 수 있습니다. 에

//I can retrieve any value easily within that table, but cannot seem to extract the table 
//Within it named "properties", i can access the table, but cannot extract the strings  inside 

if(lua_istable(L, 2)) { 
    lua_getfield(L, 2, "canInflate"); // Let's extract the value for the key 'someKey'. Pushes the value on the top of the stack 
    static int canInflate = lua_toboolean(L, -1); // get the value of bool now at the top of stack (index: -1) 

    //printf("can inflate is %d\n", canInflate); 
    //lua_pop(L, 1); // pop the value now that we are done with it 
} 


//try to get the properties table 
if (lua_istable(L, 2)) { 
    lua_getfield(L, 2, "properties"); 

    const char *str = lua_tostring(L, -1); 

    printf("properties 1 = %s\n", str); // NULL 

    lua_pop(L, 2); 
} 

어떤 도움 :

--Function 
createSomething("wheel", { canInflate = true, properties = { "large", "full" } }) 

C 사이드 : 여기

는 지금까지이

루아 사이드 (순전히 내 머리가 튀긴 시도)가 무엇인가 이것은 크게 감사 할 것입니다

답변

3

당신이 겪고있는 문제는 당신이 루아에서 테이블을 지정하는 방법을 함께 :

t = { 'full','large'} 
t = { [1] = 'full', [2] = 'large'} 
t={};t[1]='full';t[2]='large' 

당신이 원하는 것이 이루어집니다로 (대신 값의 키와 문자열을 사용하는 것입니다 : 다음 세 문장이 정확히 같은 결과를 코드 및 위의 샘플에서) :

문자열을 키로 사용하면 C 코드가 작동합니다.

+0

실은 그 덕분에 일했습니다! 그것이 단순한 무엇인지 알았습니다 – PersuitOfPerfection

+2

@ Dan : 그렇다면 문제가 해결되면이 대답을 받아들입니다. 왼쪽의 녹색 체크 표시입니다. –

관련 문제