• Networking a lot of stuff, what is the best way? Is caching useless?
    2 replies, posted
Hello everyone I am trying to optimize my networking code and make it smooth and clean but I don't really know what method is the best one in this case. I am currently sending a table full of variables from the server to the client, and I have around 10 different variables that I want to send. I don't want the client to be able to touch these variables hence why I am not defining them on the shared realm. ( assuming that clients can run client-sided scripts on their own ) Example of what I am doing: -- server example function SendVarsToClient(pl) local dt = {} dt.money = pl.money -- dt.money2 = pl.money2 dt.c_money = config.startmoney --- and so on -- Do keep in mind that this table holds about 10 variables -- Also I am not sending everything in the "config" table nor -- everything that the player has since I only need some of the -- variables and not all. And it is about 10 variables net.Start("someuniquenetworkstring") net.WriteTable(dt) net.Send(pl) end -- client example net.Receive( "someuniquenetworkstring", function(len, pl) local dt = net.ReadTable() -- then I open a derma thingie that displays all of it end) Is this a good method for sending a lot of variables to the client? Or should I just make the variables shared? But wouldn't that be a security risk? Do note that I only send these variables ones ( the "SendVarsToClient" function isn't called in a loop or something ). Now when I run the "SendVarsToClient" again I wouldn't want to send the same variables if they have the same value/contents. Instead I could cache all the variables on the client and keep a memory on the server of what it sent and then compare the new data table with the old and exclude those variables who have the same value/contents. Example: -- shitty server example function SendVarsToClient(pl) local dt = {} dt.money = pl.money -- dt.money2 = pl.money2 dt.c_money = config.startmoney --- and 10 more variables... pl.dt = dt -- keep a record of what was sent and to whom if( pl.count == nil ) then pl.count = 1 else pl.count = pl.count + 1 end -- keep record of how many times if( pl.count > 1 ) then -- dont want to check the cache if this is the first time -- then remove the variables that hasn't changed for k, v in pairs( dt ) do -- loop through all of the variables for k2, v2 in pairs(table.GetKeys(pl.dt)) do if( k == k2 ) then -- check if they already exist in the record if( v == v2 ) then table.remove( dt, k ) end -- check if they have the same value; if so remove end end end end net.Start("someuniquenetworkstring") net.WriteTable(dt) net.Send(pl) end -- client example net.Receive( "someuniquenetworkstring", function(len, pl) local dt = net.ReadTable() client_stuff = client_stuff or {} client_stuff.cache = table.Merge( client_stuff.cache, dt ) -- update the updated variables -- example of what it does: -- v1 = 1 | nv1 = 1 -- v2 = 5 | nv2 = 2 -- and it would update only this variable ( v2 ) so it would be equal to 2 -- v3 = 7 | nv3 = 7 -- the others stay the same --- then use the data ... end) The above code is a quick example that I wrote ( might not even work haven't tested it ) but would I benefit from caching stuff or is it just a waste of process time? Any ideas or suggestions?
are the variables inside the tables all integers? or could they have other datatypes?
They have other datatypes
Sorry, you need to Log In to post a reply to this thread.