Personal NWvars replacement

Hey guys. I coded a plugin for my gamemode. It replaces NWVars.
It is for GMod 13 only though.

nwvars.lua ( server side):

cl_nwvars.lua (client - side):

Feel free to use it in any script but give me credits.

Credits to people who helped me:
Divran - pointing out mistakes and suggesting solutions, not being an ass
MadDog - a good suggestion which I’m working on

Note: Both files have to be loaded first (right after the init/cl_init) or there must be no nwvars set before the files have loaded. Else the set nwvars will break.

If you are seeing no point in this, get out instead of rating me boxes and being** a pure idiot.**

At the bottom, it should be _R.Entity.SetNetworkedBlah = _R.Entity.SetNWBlah.

This wouldn’t even work as players who join after you don’t get your info.

Don’t get my info? What are you talking about? There’s a hook that sends the stuff to them.

Also it would be really nice if you said something like “Nice” or “I like it” instead of pointing out unexisting error ( “Players won’t get your info” ).

Why so defensive? If anyone levels criticism towards your work it’s usually because they want to help. You won’t find “trolls” who take the time to read through your script.

IIRC the new net library exists because it’s more efficient than networked variables. I haven’t tackled networking in GLua though, can someone confirm/deny? It’s a hole in my knowledge I’d like to mend.

You’re using table.Add to try to add the player or entity to a table called “nwents”, which won’t work. So Chessnut was correct.

Name: table.Add( dest, source )
Desc: Unlike merge this adds the two tables together and discards keys.
function table.Add( dest, source )

// At least one of them needs to be a table or this whole thing will fall on its ass
if (type(source)!='table') then return dest end

if (type(dest)!='table') then dest = {} end

for k,v in pairs(source) do
	table.insert( dest, v )

return dest


Line 8 will trigger and nothing will happen.

I made this replacemnt using the net library cause it (should) improve performance in scripts that rely on NW vars and it is aslo easier to write one simple function instead a few lines of code and then calling net.Receive on the other side.

@ Divran: Ok, looking into the issue.


Ok, I think I fixed it. I also made it so that if there are nwvars stored in a player and the player disconnects, the script won’t attempt to send these nwvars to the next player joined.

I will make actual NWInt functions but it might not be possible to post them today.

[editline]25th April 2012[/editline]

Updated the code. Here’s a small changelog:

  1. Removed lots of useless code on the client which would prevent this from working;
  2. Added all NWInt functions;
  3. Made it so that if a player disconnects he goes away from the nwents table in order to prevent errors;
  4. Removed table.Add. Wrote a small function to do what I needed.

It’s still wrong. What you’re doing now is copying all the values stored on the player/entity into “nwents”. You need to copy the player/entity itself, not all their values.

[lua]function _R.Entity:SetNWString( str, val )
if not nwents[self] then nwents[self] = true end

if !self.NWvars then self.NWvars = {} end
if !self.NWvars.String[ str ] then self.NWvars.String[ str ] = {} end
self.NWvars[ str ] = val
net.Start( "String" )
    net.WriteEntity( self )
    net.WriteString( str )
    net.WriteString( val )


EDIT: There’s a lot of other things that could be improved with this, but I’m going to play Starcraft 2 with some friends now :slight_smile:

another one?

Thanks, that was useful.
But Somehting is bothering me. Are you allowed to insert “self” into the brackets ()?

And yes Hentie, another one.

[editline]26th April 2012[/editline]

Updated the code.
Also Divran, I want this to work, I will improve it myself.
My point is that I would love to hear anything positive rather than just pointing out errors. I mean, I appriciate it but please say something good. I have put time and effort into this.

Yes. It’s more efficient than looping through the entire table just to see if this player/entity is in nwents or not. But you will most likely have to change the PlayerInitialSpawn loop to reflect this change.

EDIT: Nevermind, you already did.

Updated the code. made a huge optimization. I had used net.Broadcast in the InitialSpawn hook by a mistake.
I want to ask garry to PLEASE get back the simple editor for OP. The current one fucks up everything (see OP). I will fix it later cause I’m lazy and I don’t have time right now.

it seems useless to use the net library where usermessages can still be used

Net library is less overhead iirc

I was actually thinking about that. I mean if you are sending 64 fucking KBs of data then you are doing something wrong. I will fix it.
Does anyone see any use of this? I use it in my script but I want to see someone else using this code.

You can send much more data with the net library iirc

You shouldn’t be sending more than 256 bytes of data for a single networked variable in either case.

Why not, exactly? How many dial-up Garry’s Mod players are there?

Because it is stupid?

You do realise that you are kinda requesting limitation? Fuck logic!

if you are using NWVars with more than 256 bytes of data you’re really doing something wrong/shouldn’t be using NWVars