Personal NWVars

Personal NWVars, an NWVar that is efficiently sent to a single player.

Might be useful for someone, made for my friend earlier today. :tinfoil:

[lua]
// SetPNWVar - Seth
AddCSLuaFile(‘autorun/setpnwvar.lua’);

VARTYPE_NONE = 0;
VARTYPE_ANGLE = 1;
VARTYPE_VECTOR = 2;
VARTYPE_BOOL = 3;
VARTYPE_INT = 4;
VARTYPE_STRING = 5;
VARTYPE_ENTITY = 6;

function PNWType(val)
if(type(val) == ‘Angle’) then
return VARTYPE_ANGLE, umsg.Angle;
elseif(type(val) == ‘Vector’) then
return VARTYPE_VECTOR, umsg.Vector;
elseif(type(val) == ‘boolean’) then
return VARTYPE_BOOL, umsg.Bool;
elseif(type(val) == ‘number’) then
return VARTYPE_INT, umsg.Long;
elseif(type(val) == ‘string’) then
return VARTYPE_STRING, umsg.String;
elseif(type(val) == ‘NPC’ || type(val) == ‘Entity’ || type(val) == ‘Player’) then
return VARTYPE_ENTITY, umsg.Entity;
end

return VARTYPE_NONE;

end

if(SERVER) then
function _R.Player:SetPNWVar(name, val)
if(!name || !val) then
Error(’:SetPNWVar(name, val) - invalid arguments passed!’);
return;
end

    local vtype, umfunc = PNWType(val);
    if(vtype == VARTYPE_NONE || !umfunc) then
        Error(':SetPNWVar(name, val) - Val is an invalid type!');
        return;
    end
    
    self.PNWVars = self.PNWVars || {};
    
    self.PNWVars[name] = val;
    
    umsg.Start('pnwvar', self);
    umsg.Char(vtype);
    umsg.String(name);
    umfunc(val)
    umsg.End();
end

function _R.Player:GetPNWVar(name)
    return self.PNWVars && self.PNWVars[name] || nil;
end

elseif(CLIENT) then
usermessage.Hook(‘pnwvar’, function(um)
LocalPlayer().PNWVars = LocalPlayer().PNWVars || {};

    local vtype = um:ReadChar();
    local name = um:ReadString();
    
    if(vtype == VARTYPE_ANGLE) then
        LocalPlayer().PNWVars[name] = um:ReadAngle();
    elseif(vtype == VARTYPE_VECTOR) then
        LocalPlayer().PNWVars[name] = um:ReadVector();
    elseif(vtype == VARTYPE_BOOL) then
        LocalPlayer().PNWVars[name] = um:ReadBool();
    elseif(vtype == VARTYPE_INT) then
        LocalPlayer().PNWVars[name] = um:ReadLong();
    elseif(vtype == VARTYPE_STRING) then
        LocalPlayer().PNWVars[name] = um:ReadString();
    elseif(vtype == VARTYPE_ENTITY) then
        LocalPlayer().PNWVars[name] = um:ReadEntity();
    else
        return;
    end        
end);

function _R.Player:GetPNWVar(name)
    return self.PNWVars && self.PNWVars[name] || nil;
end

end
[/lua]

http://f.seth.im/setpnwvar.lua

It’s not really networked, it just sends an usermessage.

[editline]sometime[/editline]

evereybody who rates me dumb doesn’t know what their talking about.

The point is so we can set networked variables to a player without sending them to everyone else. Half of the time that is useless.

It’s useful for some, im just pointing out that it’s just sending an usermessage.

Reminds me of how hunts did his networking in PERP2 series and narwhal.

Ok?

hunts probably stole it from silverblu :downs:

A usermessage is a form of networking.

http://www.facepunch.com/fp/ratings/box.png

So 2-3 years ago hunts stole code that didn’t exist?

Different type of networking.

A networked var is readily available to client and server.

that’s the joke…

This works exactly the same, what is your point?

I wasn’t saying that the end result of the OP was any different.

How do you think networks variables work under the hood?

As far as I know, GMod doesn’t actually have legitimate “networked variables” (GetNWVar/SetNWVar) as far as the Source SDK definition of it goes. I tried implementing them myself, you can’t just create them on the fly, they’re in the class definitions.

From my experience, this seems like more or less the exact same thing that network variables are for Lua in GMod at the C level. It’s all usermessages, “network variable” or not.

VoiDeD rated you dumb so you must be wrong

Nice and simple:smile:
2 things though:

  1. you can’t send a Weapon

[lua]
function _R.Player:GetPNWVar(name)
return self.PNWVars && self.PNWVars[name] || nil; – the || nil is not needed
end
[/lua]
/nitpicking

Also can someone answer the biggest question in this thread; do you say a usermessage or an usermessage?

A usermessage > An usermessage.

thank you