int32_t LuaInterface::luaDoPlayerSendChannels(lua_State* L) // player open channel changes
{
//doPlayerSendChannels(cid[, list])
ChannelsList channels;
uint32_t params = lua_gettop(L);
if(params > 1)
{
if(!lua_istable(L, -1))
{
errorEx("Channel list is not a table");
lua_pushboolean(L, false);
return 1;
}
lua_pushnil(L);
while(lua_next(L, -2))
{
std::cout << "\nTest: " << (uint16_t)lua_tonumber(L, -2) << " - And: " << lua_tostring(L, -1); // this is what we are looking at for this test
channels.push_back(std::make_pair((uint16_t)lua_tonumber(L, -2), lua_tostring(L, -1)));
lua_pop(L, 1);
}
lua_pop(L, 1);
}
ScriptEnviroment* env = getEnv();
if(Player* player = env->getPlayerByUID(popNumber(L)))
{
if(params < 2)
channels = g_chat.getChannelList(player);
player->sendChannelsDialog(channels);
lua_pushboolean(L, true);
return 1;
}
errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
lua_pushboolean(L, false);
return 1;
}
And this is what the Lua looks like:
1 2 3 4 5 6 7 8 9 10
local testTable = {
[344] = "first",
[365] = "second",
[376] = "third",
[398] = "fourth",
[401] = "fifth"
}
function test(cid)
doPlayerSendChannels(cid, testTable)
end
And it prints this:
1 2 3 4 5
Test: 376 - And: third
Test: 398 - And: fourth
Test: 344 - And: first
Test: 401 - And: fifth
Test: 365 - And: second
As I said I am new to C++ and I am at my wits end on this one. I was thinking maybe I could sort it somehow in C++ but I have no idea how. Also another person suggested using a table in Lua like this may hold the order:
The problem with that solution is I don't know how to make the function work with a table that looks like that, if I try to use it now I get the error:
1 2
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct NULL not valid
Two things: A Lua table is almost certainly a dictionary (hash, unordered_map), so the order is dependent on the hash of the key and the buckets, not the key itself. Second, you cannot do this std::string foo(NULL);, which is what that exception is complaining about. This question is best asked on a Lua list, not a "General C++ Forum" such as this.