• 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++ Problem with getField

kubernik

Active Member
Joined
Jul 4, 2014
Messages
99
Solutions
7
Reaction score
38
Hello, i have problem with rewrite function from tfs 0.3.6 to tfs 1.3(downgraded to 8.6)

Function from 0.3.6:

C++:
int32_t LuaInterface::luaSendChannels(lua_State* L)
{
    ScriptEnviroment* env = getEnv();
    if(!lua_istable(L, -1))
    {
        errorEx("channel list is not a table.");
        lua_pushboolean(L, false);
        return 1;
    }

    std::string name;
    map16_string channels;
    lua_pushnil(L);
    while(lua_next(L, -2))
    {                           
        channels[(uint16_t)getField(L, "id")] = getFieldString(L, "name");
        lua_pop(L, 1);
    }
    lua_pop(L, 1);

    Player* player = env->getPlayerByUID((uint32_t)popNumber(L));
    if(!player)
    {
        errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
        lua_pushboolean(L, false);
    }

    player->sendChannels(channels);
    lua_pushboolean(L, true);
    return 1;
}
I wrote this code into this form:

C++:
int LuaScriptInterface::luaSendChannels(lua_State* L)
{
    //luaSendChannels(cid, {channels})
    if (!isTable(L, -1)) {
        reportErrorFunc("channel list is not a table.");
        lua_pushboolean(L, false);
        return 1;
    }
    std::string name;
    map16_string channels;
    lua_pushnil(L);
    while (lua_next(L, -2) != 0) { 
        channels[getField<uint16_t>(L, -2, "id")] = getFieldString(L, -1, "name");
        lua_pop(L, 1);
    }
    lua_pop(L, 1);

    Player* player = getUserdata<Player>(L, 1);
    if(!player) {
        reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
        lua_pushboolean(L, false);
    }

    player->sendChannels(channels);
    lua_pushboolean(L, true);
    return 1;
}

in Lua i using this script:
Lua:
function onStepIn(cid, item, pos)
    local channelsTbl = {}
    local tempTbl = {id = 1, name = "text"}
    table.insert(channelsTbl, tempTbl)
    cid:sendChannels(channelsTbl)
end

but i have error in console:
Code:
invalid key to 'next'

i think that problem is with getField, but i don't know how to use that..
I don't know how to make it work, someone can help me? :(
 
Back
Top