Networking more efficiently - Methods.

Hello Facepunch,

I’m wondering if anyone could give me any sources to achieve what I’m trying to do here. I’m assigning a value to a player, then send the variable to the client what we assigned on him. This is currently what I have, if you could help in anyway, making this more efficiently.

Instead of using Network Variables ( since they’re horribly inefficient).


local function AssignValue( ply )
	ply.myValue = 1 // <- Here we assign a value.
	sendToclient( ply ) <-- Pass through the argument
hook.Add("PlayerInitialSpawn", "AssignSomeValue_", AssignValue)

function sendToclient( ply ) <-- 'ply' Since we referenced it above, we can use it here.

		net.WriteInt(ply.myValue, 32) -- <- Here I'm trying to write the valu which we assigned to the player.
		net.WriteEntity( ply ) -- <- We want to show it to the player on his hud, so we do this.
	net.Send( ply ) -- <- We send it to the client himself.


Help is much appreciated, much thanks.

Well depends on how large is the number you can probably lower the int thats being sent.
And looks good.

this is microoptimization, networkvars are not horribly inefficient

if you really REALLY want to send anything just use a standard double or a 32 bit int - it’s only one variable and unless it’s changed 250 times a tick on a 66 tick server you will be fine

NWVars are inefficient, however, NW2/DTVars are much better in comparison. Using plain net messages just over complicates things for minimal to any gain.

Depending on the largest possible value “ply.myValue” could have, you can lower the bit count.
Also, why do you need to tell the player you sent the message to, who the player the message was sent to is? The player you sent the message to already knows who the message was sent to, himself…

nwvars aren’t inefficient, they are just sent to the client every so often to ensure the clients have the right value.

as i said on my last post changing bitcount on something this small will not help you in any way shape or form - all net messages already have an overhead of 4 bytes, adding 4 more bytes will not harm anything unless you send 80gazillion net messages a second, please stop with all the terrible coding practices!

In a question about microoptimization, suggesting a microoptimization is perfectly fine.

For single values I use SetNW2TYPE, but for chunks of data I usually do a networked version of AccessorFunc by hand using the net library. Try to only update as needed for efficiency. When you want to really microoptimize set the U/Int bit sizes by hand and avoid using WriteTable or WriteType.

NW vars v1 is quite awful for how heavy it became used. It overloads the engine when you have too many and causes delays in updates with big servers. It used to cause errors and crashes on connect because the client would be so overloaded with data upon spawning.

microoptimizing does nothing and should never be considered for anything at any point in anyone’s program

i am trying to tell the op to not worry about the 4 bytes that could potentially be used - others should do the same. worrying about 4 bytes being used every x(where x > 0.1) second is like worrying about a desktop background making your laptop’s battery die faster while you play a game

Microoptimizing doesn’t have to be done at all if the efficient/good practices are used in the first place, and constantly shunning people asking these “micro” questions just hinders them from ever learning those practices. I don’t see any reason why he shouldn’t be concerned about network overhead with NWVars. Now, the method he posted in the OP is more convoluted than worth, but NWVars are notably more bloated than their sister NW2Vars or DTVars, which I believe is an acceptable alternative to his issue.