Get table inside table Lua through C ++

1

I have a lua file with the following content:

Pokes_Icons = {
        ["Bulbasaur"] = {
            on = 12906,
            off = 12908,
            used = 12907,
        }
}

I'm trying to get the value of "on" in C ++, I tried it like this:

enum Pokes_Stats_t
{
    OFFENSE = 0,
    DEFENSE,
    SPECIALATTACK,
    SPECIALDEFENSE,
    VITALITY,
    AGILITY,
    ICONON,
    ICONOFF,
    ICONUSE,
    TYPEONE,
    TYPETWO
};

std::map <std::string, std::map <Pokes_Stats_t, int32_t> > pokeStat;
bool loadPokes(){
lua_State* L = lua_open();
std::string pokeName = "";
    if(!L){
        return false;
    }

    if(luaL_dofile(L, "Poke_Config.lua"))
    {
        lua_getglobal(L, "Pokes_Icons");
        lua_pushnil(L);
        while(lua_next(L, -2) != 0) {
            if(lua_istable(L, -1) && lua_isstring(L, -2)){ 
                pokeName = lua_tostring(L, -2);
                lua_pushnil(L);
                while(lua_next(L, -2) != 0) {
                    if(lua_isnumber(L, -1) && lua_isstring(L, -2)){
                        if(lua_tostring(L, -2) == "on"){
                            pokeStat[pokeName][ICONON] = (int32_t)lua_tonumber(L, -1);
                        }else if(lua_tostring(L, -2) == "off"){
                            pokeStat[pokeName][ICONOFF] = (int32_t)lua_tonumber(L, -1);
                        }else if(lua_tostring(L, -2) == "used"){
                            pokeStat[pokeName][ICONUSE] = (int32_t)lua_tonumber(L, -1);
                        }
                    }
                lua_pop(L, 1);
                }
            }
        lua_pop(L, 1);
        }
    }
    lua_pop(L, 1);
    std::cout << pokeName << ": " << pokeStat["Bulbasaur"][ICONUSE] << std::endl;
    if(L)
        lua_close(L);
}

But returns:

Bulbasaur: 0

Where is the error?

Edit:

I realized that I can not save the values in Map, but I still can not solve them.

    
asked by anonymous 30.03.2015 / 18:31

1 answer

1

The error is in this:

if(lua_tostring(L, -2) == "on"){

The right thing to do is:

if((std::string)lua_tostring(L, -2) == "on"){
    
01.04.2015 / 17:57