Get table inside table Lua through C++

Asked

Viewed 90 times

1

I have a moon file with the following contents:

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

I am trying to get the value of "on" in C++, I tried as follows:

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 am not able to save the values in Map, but I still can not solve.

1 answer

1

The mistake is in this:

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

The right thing is:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.