• Networked Variables
    27 replies, posted
Hey, thanks for taking the time to read this, i was wondering why everyone says networked variables are bad optimization wise. Can someone help me understand why, preferably by telling me what they do to set the variables step by step function wise, because im planning on making my own lil networked variables type of system, to improve optimization in my scripts. ( It was made very clear that im a idiot for using networked variables ). Sorry if this is a dumb question, i just dont want to remake my own system and have it worse, or the same as Networked Variables, cause then it would defeat the point of me even trying. Thanks again for taking the time to read this and please dont bash me for not knowing, its not anywhere on the wiki , and niether is the fact that networked variables are un optimized.
i believe it's because networked variables are networked constantly rather than when you set it or when the object moves into your PVS.
I use tons of networked vars, wondering the same.
If you are using a SENT or SWEP, use [url]http://wiki.garrysmod.com/page/Networking_Entities[/url]. If you are updating your data a [i]lot[/i], use the net library. If you are setting variables not very often and only have very few (and not long strings) and can't use SetupDataTables, use NWVars because code readability. Variables set with ENT:SetupDataTables and self:NetworkVar are networked with the entity itself. These are the best and will work properly with prediction if the rest of your code is good. Variables sent via the net library will never be sent more often than necessary (supposing you code it right). Variables set with NWVars will just work.
NWVars as stated in the OP are constantly being networked, even when they're not being changed. It's putting un-needed stress on the server. Using the net library to make your own variable system will make it so when the value is networked only when changed, Massively cutting down the amount of data going between server > client and vice versa. Making your own variables system will take a little bit of knowledge, dedication and determination, but it's do-able.
[QUOTE=AIX-Who;49206133]NWVars as stated in the OP are constantly being networked, even when they're not being changed. It's putting un-needed stress on the server. Using the net library to make your own variable system will make it so when the value is networked only when changed, Massively cutting down the amount of data going between server > client and vice versa. Making your own variables system will take a little bit of knowledge, dedication and determination, but it's do-able.[/QUOTE] I mean i understand how to do it, i was just worried i was gonna do what the default system did, who the hell thought itd be a good idea to constantly send the data back and forth.. lol
[QUOTE=AIX-Who;49206133]Making your own variables system will take a little bit of knowledge, dedication and determination, but it's do-able.[/QUOTE] Or just use [URL="https://github.com/SuperiorServers/plib_v2/blob/master/lua/plib/libraries/nw.lua"]mine[/URL], I doubt there's anything faster out there.
[QUOTE=StonedPenguin;49206365]Or just use [URL="https://github.com/SuperiorServers/plib_v2/blob/master/lua/plib/libraries/nw.lua"]mine[/URL], I doubt there's anything faster out there.[/QUOTE] Is there documentation or should i just read through it?
[QUOTE=DamienTehDemo;49207018]Is there documentation or should i just read through it?[/QUOTE] [code] local var = nw.Register 'MyVar' -- You MUST call this ALL shared var:Write(net.WriteUInt, 32) -- Write function var:Read(net.ReadUInt, 32) -- Read function var:SetLocal() -- Optionally set the var to only network to the player its set on var:SetGlobal() -- Registers the var for use with nw.SetGlobal var:SetNoSync() -- Stops the var from syncing to new players, SetLocal does this for you. var:Filter(function(ent, value) -- Sets a var to only send to players you return in your callback return player.GetWhatever() -- return table players end) nw.WaitForPlayer(player, callback) -- Calls your callback when the player is ready to recieve net messages -- Set Functions ENTITY:SetNetVar(var, value) nw.SetGlobal(var, value) -- Get functions ENTITY:GetNetVar(var) nw.GetGlobal(var) [/code]
Do i have permission to distribute that with my code? Or how should i go about doing that? Im pretty confused on how to use it correctly, sorry i must be overthinking it. Do i have to register each variable every time i want to set it, and what does it mean by call it in all shared. Sorry i must be like overthinking it, but im confused. O_o
[QUOTE=DamienTehDemo;49210124]Do i have permission to distribute that with my code? Or how should i go about doing that?[/QUOTE] [URL="https://github.com/SuperiorServers/plib_v2/blob/master/LICENSE"]yep[/URL] [QUOTE=DamienTehDemo;49210124] So i have to call of that every time i want to set a variable..? Like to register it and everything? [/QUOTE] Just once.
[QUOTE=StonedPenguin;49210216][URL="https://github.com/SuperiorServers/plib_v2/blob/master/LICENSE"]yep[/URL] Just once.[/QUOTE] So i just pop that in a shared lua file and i can start using the nwsystem? ( After i put it on the server ofc ). OR just once for every variable? Sorry for all the questions ;-; [editline]29th November 2015[/editline] Did some research and looked into how darkrpvars work, i think i understand howyours works sort of, with the registering and such [editline]29th November 2015[/editline] [CODE] --currentBusPoint --nextBusPoint --currentBusPointInt --nextBusPointInt --busdriving --currentBusPoint --nextBusPoint -- ply:SetNWBool("busdriving",true) -- ply:SetNWVector("currentBusPoint",busPoints[1]) -- ply:SetNWVector("nextBusPoint",busPoints[2]) -- ply:SetNWInt("currentBusPointInt",1) -- ply:SetNWInt("nextBusPointInt",2) -- ply:SetNWBool("busdrivingcooldown",false) busdrivingvar = nw.Register 'busdriving' busdrivingvar:Write(net.WriteBool) busdrivingvar:Read(net.WriteBool) currentBusPointvar = nw.Register 'currentBusPoint' currentBusPointvar:Write(net.WriteVector) currentBusPointvar:Read(net.WriteVector) nextBusPointIntvar = nw.Register 'nextBusPointInt' nextBusPointIntvar:Write(net.WriteVector) nextBusPointIntvar:Read(net.WriteVector) currentBusPointIntvar = nw.Register 'currentBusPointInt' currentBusPointIntvar:Write(net.SetNWInt, 32) currentBusPointIntvar:Read(net.SetNWInt, 32) nextBusPointIntIntvar = nw.Register 'nextBusPointIntInt' nextBusPointIntIntvar:Write(net.SetNWInt, 32) nextBusPointIntIntvar:Read(net.SetNWInt, 32) busdrivingcooldownvar = nw.Register 'busdrivingcooldown' busdrivingcooldownvar:Write(net.WriteBool) busdrivingcooldownvar:Read(net.WriteBool) [/CODE] Is this what i would put in the shared file of my script to be able to use the library and to set those vars?
that works
[QUOTE=StonedPenguin;49210761]that works[/QUOTE] -SNIP- [DEL] We shall not speak of what was orignally posted in this reply. >_>[/DEL] Nevermind i was right, it wasnt working, see anything wrong? [url]https://gyazo.com/de6970fdb83c8afe3ae45d5ed4ff2ca3[/url] [url]https://gyazo.com/8dd6cdb18073691ff6368516f8f954cd[/url]
[QUOTE=DamienTehDemo;49210806]-SNIP- [DEL] We shall not speak of what was orignally posted in this reply. >_>[/DEL] Nevermind i was right, it wasnt working, see anything wrong? [url]https://gyazo.com/de6970fdb83c8afe3ae45d5ed4ff2ca3[/url] [url]https://gyazo.com/8dd6cdb18073691ff6368516f8f954cd[/url][/QUOTE] I might be completely wrong here, but would the code not be like: [lua] busdrivingvar = nw.Register 'busdriving' busdrivingvar:Write(net.WriteBool) busdrivingvar:Read(net.WriteBool) currentBusPointvar = nw.Register 'currentBusPoint' currentBusPointvar:Write(net.WriteVector) currentBusPointvar:Read(net.ReadVector) nextBusPointIntvar = nw.Register 'nextBusPointInt' nextBusPointIntvar:Write(net.WriteVector) nextBusPointIntvar:Read(net.ReadVector) currentBusPointIntvar = nw.Register 'currentBusPointInt' currentBusPointIntvar:Write(net.SetNWInt, 32) -- Are these even functions? currentBusPointIntvar:Read(net.GetNWInt, 32) -- Are these even functions, Should it not be like this one below: nextBusPointIntIntvar = nw.Register 'nextBusPointIntInt' nextBusPointIntIntvar:Write(net.SetNWInt, 32) nextBusPointIntIntvar:Read(net.GetNWInt, 32) busdrivingcooldownvar = nw.Register 'busdrivingcooldown' busdrivingcooldownvar:Write(net.WriteBool) busdrivingcooldownvar:Read(net.ReadBool) [/lua] I've not read the code, or seen any documentation. It's just a stab in the dark. You had the read functions the same as the write function. This below might work [lua] busdrivingvar = nw.Register 'busdriving' busdrivingvar:Write(net.WriteBool) busdrivingvar:Read(net.WriteBool) currentBusPointvar = nw.Register 'currentBusPoint' currentBusPointvar:Write(net.WriteVector) currentBusPointvar:Read(net.ReadVector) nextBusPointIntvar = nw.Register 'nextBusPointInt' nextBusPointIntvar:Write(net.WriteVector) nextBusPointIntvar:Read(net.ReadVector) currentBusPointIntvar = nw.Register 'currentBusPointInt' currentBusPointIntvar:Write(net.WriteUInt, 32) currentBusPointIntvar:Read(net.ReadUInt, 32) nextBusPointIntIntvar = nw.Register 'nextBusPointIntInt' nextBusPointIntIntvar:Write(net.WriteUInt, 32) nextBusPointIntIntvar:Read(net.ReadUInt, 32) busdrivingcooldownvar = nw.Register 'busdrivingcooldown' busdrivingcooldownvar:Write(net.WriteBool) busdrivingcooldownvar:Read(net.ReadBool) [/lua] If / when you sell this on scriptfodder, please make sure you can actually support the script. At the minute it looks like you may struggle.
I suppose I should have fully looked at that. net.SetNWInt? What?..
Whats the diffrence between uint and int, also , i found out that i did that with the functions afterwards, felt like a idiot xD
[QUOTE=DamienTehDemo;49211553]Whats the diffrence between uint and int, also , i found out that i did that with the functions afterwards, felt like a idiot xD[/QUOTE] Signed vs unsigned
Whats the difference between those 2?
Signed can be negative but is limitted to a smaller number. Unsigned can only be positive but can be quite larger. I don't know the exact sizes off the top of my head.
[QUOTE=skullorz;49206070]i believe it's because networked variables are networked constantly[/QUOTE] [QUOTE=AIX-Who;49206133]NWVars as stated in the OP are constantly being networked, even when they're not being changed. It's putting un-needed stress on the server.[/QUOTE] So you are telling me that when I have 10 global NWStrings that are 200bytes long each, it's gonna send me 2kb "constantly". I'm pretty sure we can all agree that this is not the case. [QUOTE=DamienTehDemo;49206038]i was wondering why everyone says networked variables are bad optimization wise. Can someone help me understand why, preferably by telling me what they do to set the variables step by step function wise[/QUOTE] Because most facepunch users probably still think that the old NWVar system is in place. It was replaced by _Kilburn's new system: [QUOTE] The previously obsolete NWVars have been remade from scratch and now provide the same features and speed benefits as NetworkVars (formerly known as DTVars) [/QUOTE] NWVars are perfectly fine to use, and they probably outperform most (if not all) custom made NWVar systems. The only real issue that you have with NWVars is that they are networked with everyone. That means that if you set a NWVar that really only one (or few) clients need to know about, you are possibly wasting an immense amount of bandwidth. Sometimes you might not want other players to know some information either (like if you are a traitor in TTT). In these cases NWVars are obviously bad and shouldn't be used (that's where you should use your own NWVar system that does private variables).
[QUOTE=syl0r;49214992]Because most facepunch users probably still think that the old NWVar system is in place. It was replaced by _Kilburn's new system: [/QUOTE] I have to disappoint you there, its still the old stuff since nobody bothered to properly test it on the development branch it had to get reverted as it broke some things.
[QUOTE=syl0r;49214992]Text[/QUOTE] What i'd been told is that the NWVars system networks the value not constantly but more than it needs to, e.g when the value has not been changed, it's still being networked to clients who already know about it. As said in the OP, The Re-done NWVars had been reverted due to them causing issues, so custom NWVar systems are probably slightly better, until _Killburns ones are fixed. I'd still recommend people use systems like the one that StonedPenguin has made, as they are most likely better for performance ( On the current gmod update ).
Since my system adds a net receiver for every var to eliminate the overhead of sending the var name and local vars are just as fast as directly sending a net message I'd imagine it'd be by far faster then NWVars for most reasonable uses.
That also means you can't sell it on ScriptFodder if you use someone elses code due to support issues Damien, not just because you have permission to sell it.
[QUOTE=StonedPenguin;49215988]Since my system adds a net receiver for every var to eliminate the overhead of sending the var name and local vars are just as fast as directly sending a net message I'd imagine it'd be by far faster then NWVars for most reasonable uses.[/QUOTE] Out of curiosity, did you ever do actual benchmarks? One test would be for example setting CurTime to check the time needed to arrive. The other thing is NWVars do not use strings to identify variables they use string tables where the index of the string is used, so your system is already having overhead. util.AddNetworkString is doing exactly that also, so per custom NWVar you have twice as much data as original NWVars. Not to mention the entire net library includes also a header per message you probably can't do much about that. Edit: Also NWVars do not constantly send their value, it only happens if they change the value so you might want to reconsider how your system works if its constantly sending something because u keep updating the variable unless its wanted for prediction purposes.
[QUOTE=Zeh Matt;49219617]Out of curiosity, did you ever do actual benchmarks? One test would be for example setting CurTime to check the time needed to arrive.[/QUOTE] I have not. [QUOTE=Zeh Matt;49219617]The other thing is NWVars do not use strings to identify variables they use string tables where the index of the string is used, so your system is already having overhead. util.AddNetworkString is doing exactly that also, so per custom NWVar you have twice as much data as original NWVars. Not to mention the entire net library includes also a header per message you probably can't do much about that. [/QUOTE] It doesn't send the var name since it adds a new network string for every var. With proper usage of local vars which are as fast as directly sending a net message to the client with nothing but the data and setting the read/write functions are more then enough to make it vastly more efficient then NWvars in most cases.
[QUOTE=StonedPenguin;49219780]I have not. It doesn't send the var name since it adds a new network string for every var. With proper usage of local vars which are as fast as directly sending a net message to the client with nothing but the data and setting the read/write functions are more then enough to make it vastly more efficient then NWvars in most cases.[/QUOTE] Please do some actual tests before making a statement that it is in "most cases" more efficient. Before making such statement you should collect following information: 1. Total size of data (Yes that sadly includes the net library header) 2. The delay to arrival (Server -> Client and Client -> Server) 3. Stress tests such as registering 50 NWVars and 50 of yours, basically repeat step 1 and 2 with this. As long you can not provide such information you should not claim that it is in any way more efficient.
Sorry, you need to Log In to post a reply to this thread.