This basicly sets a int, string or bool into the player's table and sends it to the client by usermessage every time it's set. It can be a replacement from using NW vars that are constantly updated. This isn't a huge project nor is it probably the /best/ system but it works fine and I use it for many of my scripts.
[b]Example:[/b]
[code]
function Spawn( ply )
ply:SetInt( "Test", 1 );
end
hook.Add( "PlayerInitialSpawn", "Spawn", Spawn );
[/code]
Then simply use
[code]
LocalPlayer():GetInt( "Test" );
[/code]
on the client.
Code(shared):
[lua]
--Int
--Bool
--String
local meta = FindMetaTable( "Entity" );
VariableTypes = { };
VariableTypes["Int"] = "Short";
VariableTypes["Bool"] = "Bool";
VariableTypes["String"] = "String";
NetworkedVars = { };
NetworkedVars.Int = { };
NetworkedVars.Bool = { }
NetworkedVars.String = { };
for k, v in pairs( VariableTypes ) do
if( SERVER ) then
meta["Set" .. k] = function( self, key, val )
if( key == nil or
val == nil ) then
return;
end
if( val == self:GetTable()[key] ) then return; end
if( not self or
not self:IsValid() ) then
return;
end
self:GetTable()[key] = val;
umsg.Start( "SetVar" .. k, self );
umsg.String( key );
umsg[v]( val );
umsg.End();
end
end
if( CLIENT ) then
usermessage.Hook( "SetVar" .. k, function( msg )
local key = msg:ReadString();
local val = msg["Read" .. v]( msg );
NetworkedVars[k][key] = val;
end );
end
meta["Get" .. k] = function( self, key )
if( key == nil ) then return; end
if( not self or
not self:IsValid() ) then
return;
end
if( SERVER ) then
return self:GetTable()[key];
else
return NetworkedVars[k][key];
end
end
end
[/lua]
Whatever you want to call it, son. Go test it.
[editline]04:23PM[/editline]
To add on; it's not supposed to be shared between players. Because that's how ESPs and other clientside hack shit are used.
If you want to have other players know about other players details, use NW vars.
The main reason I've done this is because I've seen too many people use NW vars just so they can get the variable clientside too.
[QUOTE=H0rsey;22849617]Go test it.[/QUOTE]
I have yet to test it but I can see a few flaws from reading it. Alright so in no particular order :
You mean for people to use this as a replacement for NetworkedVariables but in it's current form it will break if you try to use it on anything else then a player, since you'd be sending a usermessage to an entity.
Serverside you are setting values of all types in the same table instead of separate ones like you do on the client. This means that variables with the same name but a different type will override one another on the server but not on the client.
You seem to be trying to make your code error proof. If you want to go that road you need to type() check your inputs. Entering something else then a string for a key name will cause errors for example.
Assuming you want to make your code work on something else then players you will also have to set the values on a per-entity basis clientside rather then in a global table. That is how usermessages work. It would allow the player to have more then one entity having the same networked integer set, for one thing.
VariableTypes should be local.
In your current code NetworkedVars should be a clientside table, could use with being localed.
Otherwise nice initiative. If/when you get it fixed up if you want a bit more visibility/durability you might want to create an article on the Garry's Mod wiki.
Sorry, you need to Log In to post a reply to this thread.