• usermessaging converting to netmessaging onto a table?
    8 replies, posted
So I have this from an older game mode but I cannot seem to get it to load to the table. I just cannot get it to work Ive tried to make it net-messaging but just no luck there. Can anyone help me out on this I would really appreciate it thank you. cl_usermessaging [lua] /*--------------------------------------------------------- Name: Top10Player Desc: Adds a player listing to the F4 top10 derma. ---------------------------------------------------------*/ usermessage.Hook( "Top10Player", function( um ) local name = um:ReadString() local kills = um:ReadShort() local deaths = um:ReadShort() if not kills or not deaths then Top10Players:AddLine( name, kills, deaths, "1:1" ) else Top10Players:AddLine( name, kills, deaths, math.Round( kills / deaths, 2 ) .. ":1" ) end end ) [/lua] client_vgui [lua] RunConsoleCommand( "pk_top10" ) -- Get the list. local DermaPanel = vgui.Create( "DCollapsibleCategory" ) Top10Players = vgui.Create( "DListView" ) Top10Players:SetParent( DermaPanel ) Top10Players:SetPos( 25, 50 ) Top10Players:SetSize( 450, 225 ) Top10Players:SetMultiSelect( false ) Top10Players:AddColumn( "Name" ) Top10Players:AddColumn( "Kills" ) Top10Players:AddColumn( "Deaths" ) Top10Players:AddColumn( "KDR" ) Menu:AddItem( DermaPanel ) DermaPanel:SetLabel( "Top 10 Players" ) DermaPanel:SetSize( 450, 225 ) -- Keep the second number at 50 DermaPanel:SetContents( Top10Players ) [/lua] server_commands [lua] concommand.Add( "pk_top10", function( ply ) local top10 = table.sortTop10( PK.Scores ) for _, v in pairs( top10 ) do net.Start( "Top10Player", ply ) net.ReadString( v.Name ) net.Send( v.Kills ) net.Send( v.Deaths ) net.Broadcast() end end ) [/lua]
You make the server command net messaging, but you didn't use [lua]util.AddNetworkString( "Top10Player" );[/lua] on the SERVER; and you didn't convert the client-code to a [lua]net.Receive( "Top10Player", function( len ) end );[/lua] Additionally, you're not sending the "table", you're sending every name inside the top10 array/list as individual messages. use net.WriteTable( top10 ) to send the table of data.
[QUOTE=Acecool;44523163]Additionally, you're not sending the "table", you're sending every name inside the top10 array/list as individual messages. use net.WriteTable( top10 ) to send the table of data.[/QUOTE] Other than this, acecool's right [editline]a[/editline] Also, there are a couple of things wrong with your code, just on the server: you're sending 10 messages, one for each entry in the table you're only sending the message to one player, so you don't need to broadcast you have to use Write* functions from the [url=http://wiki.garrysmod.com/page/Category:net]net library[/url] [lua]concommand.Add( "pk_top10", function( ply ) local top10 = table.sortTop10( PK.Scores ) net.Start( "Top10Player") for _, v in pairs( top10 ) do net.WriteString( v.Name ) net.WriteUInt( v.Kills , 7 ) --7 bits allows up to 127 kills, if kills can be negative use WriteInt instead of WriteUInt net.WriteUInt( v.Deaths , 7 ) --same thing here, up to 127 deaths end net.Send(ply) end )[/lua]
oops I forgot to put the original in there too my bad and thank you I will mess around with it but I guess the new update took out usermessaging so im not sure how to convert umsg.Short( v.Kills ) I will try your guys methods thank you I will be back tho this is completely new to me haha [lua] concommand.Add( "pk_top10", function( ply ) local top10 = table.sortTop10( PK.Scores ) for _, v in pairs( top10 ) do umsg.Start( "Top10Player", ply ) umsg.String( v.Name ) umsg.Short( v.Kills ) umsg.Short( v.Deaths ) umsg.End() end end ) [/lua] [editline]11th April 2014[/editline] okay so now I am getting stuff in the top ten players table but its like completely off not sure why I probably have the coding really wrong this is what I did and started to get some results from it. [lua] /*--------------------------------------------------------- Name: Top10Player Desc: Adds a player listing to the F4 top10 derma. ---------------------------------------------------------*/ net.Receive( "Top10Player", function( len ) local name = net.ReadString(len) local kills = net.ReadUInt(len) local deaths = net.ReadUInt(len) if not kills or not deaths then Top10Players:AddLine( name, kills, deaths, "1:1" ) else Top10Players:AddLine( name, kills, deaths, math.Round( kills / deaths, 2 ) .. ":1" ) end end) [/lua] but there is no name and the kill ratio is always at 1:1
[QUOTE=PortalGod;44523193][QUOTE=Acecool;44523163] Additionally, you're not sending the "table", you're sending every name inside the top10 array/list as individual messages. use net.WriteTable( top10 ) to send the table of data.[/QUOTE] Other than this, acecool's right[/QUOTE] How is it wrong? He's going through a table assigned to "top10" and sending a new message for each element inside the table instead of sending the table all at once. Why not just: [lua]// Server util.AddNetworkString( "Top10Player" ); concommand.Add( "pk_top10", function( ply ) local top10 = table.sortTop10( PK.Scores ); net.Start( "Top10Player" ); net.WriteTable( top10 ); net.Send(ply) end )[/lua]
I believe that PortalGods method works just this part is not reading it correctly. [lua] /*--------------------------------------------------------- Name: Top10Player Desc: Adds a player listing to the F4 top10 derma. ---------------------------------------------------------*/ net.Receive( "Top10Player", function( len ) local name = net.ReadString(len) local kills = net.ReadUInt(len) local deaths = net.ReadUInt(len) if not kills or not deaths then Top10Players:AddLine( name, kills, deaths, "1:1" ) else Top10Players:AddLine( name, kills, deaths, math.Round( kills / deaths, 2 ) .. ":1" ) end end) [/lua] I think.
When you use [B]net.ReadInt[/B] and [B]net.ReadUInt[/B], they take a parameter indicating the number of bits to read. You specify this number as the second parameter when using [B]net.WriteInt[/B] and [B]net.WriteUInt[/B]. E.g., [CODE]-- write the number '1000' using 16 bits net.WriteUInt( 1000, 16 )[/CODE] [lua]-- read the number using 16 bits net.ReadUInt( 16 )[/lua] If you write a number using 16 bits, you must read it back with 16 bits. The maximum number of bits you can read and write is 32, if I remember correctly. Alternatively, if you're not concerned about sending slightly larger messages, you can use [B]net.WriteFloat [/B]and [B]net.ReadFloat[/B], which don't require that you indicate the number of bits to read/write. E.g., [CODE]net.WriteFloat( 12345678 )[/CODE] [CODE]net.ReadFloat()[/CODE] It supports sufficiently large numbers up to roughly 2^128 - 1.
[QUOTE=Acecool;44524236]How is it wrong? He's going through a table assigned to "top10" and sending a new message for each element inside the table instead of sending the table all at once.[/QUOTE] you're not wrong about what he's doing, you're wrong about using Write/ReadTable, garry himself discourages using it because there's a lot of overhead when sending each type for each key/value. 95% of the time it can be optimized by not using Write/ReadTable, especially when you know exactly what's in the table, like in this case [QUOTE=Mista Tea;44525119]The maximum number of bits you can read and write is 32, if I remember correctly. Alternatively, if you're not concerned about sending slightly larger messages, you can use [B]net.WriteFloat [/B]and [B]net.ReadFloat[/B], which don't require that you indicate the number of bits to read/write.[/QUOTE] this is true too, but unless you're really expecting someone to have 340282366920938463463374607431768211455 kills and deaths, it's worth the extra time it takes to figure out how many bits a situation needs and use that instead
[QUOTE=PortalGod;44526729]you're not wrong about what he's doing, you're wrong about using Write/ReadTable, garry himself discourages using it because there's a lot of overhead when sending each type for each key/value. 95% of the time it can be optimized by not using Write/ReadTable, especially when you know exactly what's in the table, like in this case[/QUOTE] I can go with that; although it's not a ton of overhead. There are enumerations which stand for each type being sent, so a few bytes 1-2 of overhead per element, plus the 4 bytes or 40 it costs for a table; I defined a system for calculating exact size of table including the Lua overhead for each data-type. I'll try to find that. It can definitely be optimized by sending and receiving exact types when they're known.
Sorry, you need to Log In to post a reply to this thread.