What data transfer methods are best to use for what?
2 replies, posted
I have read different things all over the place, people saying it is good to use this, but on the wiki it says not to etc. I really need someone to clear it for me, what data transfer methods should I use when making a gamemode and when?
For instance, a 'money' variable. SetNWInt on the server? Or set a variable on the play and network it with the net library? I just don't know what the right one to use is.
I made an inventory system which uses the net library to send data for instance when picking up an item, is that right?
Those are just two examples, but I have more questions really. I don't want to code something improperly and then regret it later.
Thanks.
Generally I try to avoid using Get/SetNetworked* as they create unnecessary strain on the server
Storing a variable on the player and only networking it when it changes is the way to go about it.
so for money you'd do something like
[LUA]
local pMeta = FindMetaTable( "Player" )
function pMeta:GetMoney()
return ( self.Money or 0 )
end
if SERVER then
util.AddNetworkString( "Send_PlayerMoney" )
function pMeta:SetMoney( amt )
ply.Money = amt
self:SaveMoney()
self:NetworkMoney()
end
function pMeta:SaveMoney()
self:SetPData( "dollabillz", ( self.Money or 0 ) )
end
function pMeta:LoadMoney()
self:SetMoney( self:GetPData( "dollabillz", 0 ) )
end
function pMeta:NetworkMoney()
net.Start( "Send_PlayerMoney" )
net.WriteEntity( self )
net.WriteInt( self:GetMoney(), 32 )
net.Broadcast()
end
hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawn_SetMoney", function( ply ) ply:LoadMoney() end )
hook.Add( "PlayerDisconnected", "Money_Save", function( ply ) ply:SaveMoney() end )
else
net.Receive( "Send_PlayerMoney", function()
local ply, money = net.ReadEntity(), net.ReadInt( 32 )
ply.Money = money
end )
end
[/LUA]
and for an inventory it would be something similar, but with a table. you would also just update the individual parts of the table, not the entire thing
hope it helps
edit: forgot about this - [url]http://wiki.garrysmod.com/page/PLAYER/SetupDataTables[/url]
probably better but its like 1am so someone else can tell you what
[QUOTE=rejax;43983231]Generally I try to avoid using Get/SetNetworked* as they create unnecessary strain on the server
Storing a variable on the player and only networking it when it changes is the way to go about it.
so for money you'd do something like
[LUA]
local pMeta = FindMetaTable( "Player" )
function pMeta:GetMoney()
return ( self.Money or 0 )
end
if SERVER then
util.AddNetworkString( "Send_PlayerMoney" )
function pMeta:SetMoney( amt )
ply.Money = amt
self:SaveMoney()
self:NetworkMoney()
end
function pMeta:SaveMoney()
self:SetPData( "dollabillz", ( self.Money or 0 ) )
end
function pMeta:LoadMoney()
self:SetMoney( self:GetPData( "dollabillz", 0 ) )
end
function pMeta:NetworkMoney()
net.Start( "Send_PlayerMoney" )
net.WriteEntity( self )
net.WriteInt( self:GetMoney(), 32 )
net.Broadcast()
end
hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawn_SetMoney", function( ply ) ply:LoadMoney() end )
hook.Add( "PlayerDisconnected", "Money_Save", function( ply ) ply:SaveMoney() end )
else
net.Receive( "Send_PlayerMoney", function()
local ply, money = net.ReadEntity(), net.ReadInt( 32 )
ply.Money = money
end )
end
[/LUA]
and for an inventory it would be something similar, but with a table. you would also just update the individual parts of the table, not the entire thing
hope it helps
edit: forgot about this - [url]http://wiki.garrysmod.com/page/PLAYER/SetupDataTables[/url]
probably better but its like 1am so someone else can tell you what[/QUOTE]
Thank you very much, that helped me out. Luckily I already did the inventory system like that so I don't have to redo anything.
I wasn't aware that you could setup data tables for players like that, so I'll look into it, I thought it was only for scripted entities.
Thanks again!
Sorry, you need to Log In to post a reply to this thread.