• Sending last 30 lines of a Lua Table
    2 replies, posted
Hi all, So, I'm still on the development of my GameMode, and again I got a problem optimizing the logs view system. So like said in the title it's again a lua table related problem. Seriously, Lua table are for me a pain to use. Why don't they add any Matrix instead of their tables ? That's so far more simple to have matrix. Working with matrix is so much fun and easy... Also basicaly, like said in the title I made a function to retrieve last lines of a table. [code] //This is part of the GameGlobal library of RealisticRoleplay //It's added automaticaly by the GameMode library engine (at the Pre Startup event of the GameMode) //At the end of the file of this part, there's a DefineLibrary(LIB_TBL, LIB_NAME) which tells the GameMode to load that as library, so like I already told you, do not care about how it's loaded. //The only thing you need to care is this function. //The call context of this function is in the rpsa_concommands defines of the GameMode's rrpadmin module. You haven't to take care of the rrpadmin module, it defines just how administration is done. util.AddNetworkString("RealisticRoleplay_AdminLogsRetreiver") function GameGlobal:SendLogsToClient(Player, lastLines) //The parameter lastLines is equal to 30, when this function is being called by the GameMode's rrpadmin module. if (!AcceptLogSend) then return end if (IsValid(Player) and Player:IsSuperAdmin()) then if (lastLines == nil or lastLines < 0) then //Old un-optimized way of sending logs which will crash right after about 500 lines of logs... net.Start("RealisticRoleplay_AdminLogsRetreiver") net.WriteTable(logs) net.Send(Player) else //Instead of sending the whole table, we should just send the last few moments of the game-play. local sendTbl = {} //The table that will be sent using net.WriteTable for i = #logs, lastLines, -1 do //Broken code start table.insert(sendTbl, logs[i]) end //Broken code end net.Start("RealisticRoleplay_AdminLogsRetreiver") net.WriteTable(sendTbl) //It works, but instead sends 10 lines of logs, taken from random location in the big log table... net.Send(Player) end end end [/code] Thanks by advance, In the hope of a quick answer, Yuri6037.
If I understood you correctly then you want something like this: Sender side: [code] local function sendLastN(tbl, n) net.WriteUInt(n, 8) for i = #tbl-n, #tbl do net.WriteType(tbl[i]) end end -- USAGE EXAMPLE: local sometable = {1,2,3,4,5,6} net.Start("messagename") sendLastN(sometable, 3) -- send last 3 entries net.Broadcast() -- Send/SendToServer/whatever [/code] Receiver side: [code] local function recvLastN() local n = net.ReadUInt(8) local out = {} for i = 1, n do local ty = net.ReadUInt(8) out[#out+1] = net.ReadType(ty) end return out end -- USAGE EXAMPLE: net.Receive("messagename", function() local tbl = recvLastN() -- tbl now has 4,5,6 -- do something with tbl end) [/code]
[code] for i = #tbl-n, #tbl do end[/code] I'll try your for loop, maybe it will work as i expect. [editline]30th March 2015[/editline] Thank you that worked
Sorry, you need to Log In to post a reply to this thread.