Since everyone is complaining about SetNW* causing crashes and I happened to need to make a new lib for my server I’ve made nw-vars.

nw-vars are a simple replacement to SetNW*. Data is synced when the player first joins the server and the moment a var is changed. You can optionally set the write, read functions and a filter function to increase efficiency as seen below.

Link: https://github.com/aStonedPenguin/nw-vars

To use place in: lua/includes/modules

Example usage:

  • Get a var

  • Set a var
    ENTITY:SetNetVar(var, value)

  • Register the var, this is totally optional, filter and read/write functions are optional
    nw.Register(‘MyVar’, {
    Read = net.ReadString,
    Write = net.WriteString,
    Filter = function(ent, var, value)
    return ent

  • Use this hook instead of InitialSpawn
    hook.Add(‘PlayerEntityCreated’, ‘example’, function(pl)
    pl:SetNetVar(‘MyVar’, ‘Hello’)

Honestly I think the simple approach taken here is probably better than other networking libraries. Gives you a low level of abstraction that gets the job done without sacrificing performance for the sake of tons of automagic features.

nice edit

he totally didn’t force me to change that.

At least this one won’t cause any of my addons to break like others which replace SetNW*.

So do i need to place the nw.lua in lua/includes/modules and let it go
or should i also change SetNW to SetNetVar ?

You’ll have to overwrite SetNW* or replace all calls yourself.

This is a neat little library, I did something similar myself.

There’s a bug though, I’m not really sure if it’s something you want to address or if it was just something that I needed in my implementation.

Anyway, net messages are not guaranteed to be received in the same order that they were sent, so it’s possible on the server:

ent:SetNetVar(‘test’, 100);
ent:SetNetVar(‘test’, 50);

Will in turn, set it to 50 then 100, effectively making the second call useless.

Do you have any source for that?
If that is true that would be really bad.

NW Events are sent as binary encoded UDP packets aren’t they? There’s no ordering built into that so unless GMod / Valve implements their own ordering and retransmission system they will be vulnerable to the same loss / reordering that all UDP is.

There is ordering (unless you mark the message as unreliable).