Networked Variables

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 http://wiki.garrysmod.com/page/Networking_Entities. If you are updating your data a lot, 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.

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

Or just use mine, I doubt there’s anything faster out there.

Is there documentation or should i just read through it?



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)


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

yep

Just once.

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]


 
--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)


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

-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?

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

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.