• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

C++ "invalid key to 'next'" when trying to read a list inside a LUA STATE in C++

Shaykie2

New Member
Joined
Dec 21, 2018
Messages
17
Reaction score
0
I'm trying to add elements to the NPC Trade, so when the "luaOpenShopWindow" method is triggered I'm sending a list of IDs and Quantities, something like this:

{"id":2160,"buy":5000,"sell":0,"requiredItems":[{"count":10,"id":2152}],"subType":1,"name":"crystal coin"}
{"id":2520,"buy":1,"sell":0,"requiredItems":[{"count":1,"id":7618}],"subType":1,"name":"demon shield"}

This is my adapted code:

C++:
        lua_getfield(L, tableIndex, "requiredItems");
        if (lua_istable(L, -1)) {
            std::list<ShopRequiredItem> requiredItemsList;
            lua_pushnil(L);
            while (lua_next(L, -2) != 0) {
                const auto tableIndexReqItem = lua_gettop(L);
                if (lua_istable(L, tableIndexReqItem)) {
                    ShopRequiredItem reqItem;

                    reqItem.itemId = tfs::lua::getField<uint32_t>(L, tableIndexReqItem, "id");
                    reqItem.count = tfs::lua::getField<int32_t>(L, tableIndexReqItem, "count");

                    requiredItemsList.push_back(reqItem);
                }

                lua_pop(L, 1);
            }

            item.requiredItems = requiredItemsList;
        }


1733613184601.webp


For the first item it gets the values normally and populates the variable correctly.

Now in the second item that it goes through, whether or not there is a second item in this "requiredItems" it gives the error "invalid key to 'next'"

Can anyone help me?
 
Back
Top