 # Clever Way of Networking

One of the more common issues I’ve encountered with networking information between the server and client comes from having a variable that is changing all the time on the server that also needs to be accurate on the client ( for example, charge% of a weapon ).
When I was first learning starting out, I’d just send a net message or use NW Vars anytime the variable changed. This is however, really inefficient and can be slow if your variable is changing a lot. Even with the improved speed of net messages, if not predicted properly the variable can change at unexpected rates and times.

After giving it some thought however, I came up with a new concept for “networking” the variable that would only require 1-2 net messages to be sent even if the number is changing every tick. It’s not applicable in every scenario and is slightly more complicated to set up, but its all worth it in the end.

Essentially, this is applicable where:
• the variable is a number
• it has a constant rate of change (e.g. incr by 1 every second )
• it has a minimum and maximum which are known on the client
• the time at which it should start changing is known

So why do we need to know these? Welp, it’s pretty simple to explain with an example:

``````

-- these should be shared!
local var = 100 -- our variable
local varMax = 100
local varMin = 0
local varChargeTime = 10 -- it takes 10 seconds to recharge
local varLastChargeUsedTime = 0

function useCharge()
var = 0
varLastChargeUsedTime = CurTime()
net.Start( "sendTheTime" )
net.WriteFloat( varLastChargeUsedTime )
net.Send( someplayer )
end

-- What this function does is tell the client the last time the var changed.
-- Since we wrote the code we know that the var is always set to zero when this happens.
-- We also know that it takes 10 seconds to go all the way back to 100.
-- Using this we can make a simple equation to accurately predict the serverside variable.
-- Pretend this is client side vvv

clientVarTime = 0

end )

function getClientCharge()
return math.min( ( CurTime() - clientVarTime )/varChargeTime, 1 )*varMax
end

-- This function returns a highly accurate number without the need to constantly send data over from the server.
-- This is really helpful if your variable is updating constantly on the server, such as every tick.
-- It is especially helpful if your variable is used to calculate the proportions of anything to do with UI - it makes bars and such increase smoothly (which can be further improved with Lerping ).

``````

Basically, what you’re doing is sending the time that the variable changed to the client and then letting them do the rest.
The calculation itself takes the current time and takes away the time that the variable last changed. This gives us the time in seconds since the variable last changed. We then divide this by the time it takes for the variable to reset to its maximum, giving us a number between 0 and 1 (0 being not complete, 1 being full ).
We then multiply this by our maximum value to give us an accurate value.
I don’t know if this is common practice but I found it to be pretty helpful!

Or you could just ping the client – CurTime is always guaranteed to be the same between the server and client.

Definitely not the -same-, but it does synchronise each server tick.

CurTime() is only the same in predicted hooks.