Hello Guys.
I was working on a Networked Var function that only server can change var value Server->Client
[CODE]
function CreateNetworkVar(Object, VarName, Type, DefaultValue, NotIsHookCaller)
local ReadFunction, WriteFunction, SetFunctionName, GetFunctionName = NetworkDefine(Type, VarName)
Object[VarName] = DefaultValue
Object[GetFunctionName] = function()
return Object[VarName]
end
if SERVER then
util.AddNetworkString("Send" .. VarName)
local SetFunction = function(Value) -- Value = Object for some reason
Object[VarName] = Value
PrintTable(Value)
net.Start("Send" .. VarName)
WriteFunction(Value)
net.Broadcast()
if !NotIsHookCaller then
hook.Call("ZP" .. VarName, GAMEMODE, Value)
end
end
Object[SetFunctionName] = SetFunction
else
Object[SetFunctionName] = function(Value)
Object[VarName] = Value
if !NotIsHookCaller then
hook.Call("ZP" .. VarName, GAMEMODE, Value)
end
end
net.Receive("Receive" .. VarName, function()
Object[SetFunctionName](ReadFunction())
end)
end
end
[/CODE]
1º - There's a simple way to do this? (I know there's networkvar, but the value can be changed on client side).
2º - If not, what's wrong with my code?
1, A [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/NetworkVar]Entity:NetworkVar[/url] changed client side won't update the server side value, nor will it stay changed client side as the server will tell it to update.
Another way is with the `SetNW*` and `GetNW*` functions. If you use the second version of them then they'll work the same way as [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/NetworkVar]Entity:NetworkVar[/url] would with less work.
2, What's `NetworkDefine` and how are you calling `CreateNetworkVar`? First thing I can see is you've prefixed your net message name with "Send" on the server, but "Receive" on the client, they have to be the same.
[QUOTE=bigdogmat;52275809]1, A [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/NetworkVar]Entity:NetworkVar[/url] changed client side won't update the server side value, nor will it stay changed client side as the server will tell it to update.
Another way is with the `SetNW*` and `GetNW*` functions. If you use the second version of them then they'll work the same way as [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/NetworkVar]Entity:NetworkVar[/url] would with less work.
2, What's `NetworkDefine` and how are you calling `CreateNetworkVar`? First thing I can see is you've prefixed your net message name with "Send" on the server, but "Receive" on the client, they have to be the same.[/QUOTE]
Bigdotmat. Thanks for your response...
1 - There's a similiar way to do this with a table (Not an entity)?
2 - Forgot to change receive, I'm calling CreateNetworkVar in the second line of a .lua:
[CODE]
RoundManager = {}
CreateNetworkVar(RoundManager, "RoundState", "Number", ROUND_WAITING_PLAYERS)
CreateNetworkVar(RoundManager, "SpecialRound", "Bool", false)
CreateNetworkVar(RoundManager, "Deathmatch", "Bool", false)
CreateNetworkVar(RoundManager, "Round", "Number", 0)
[/CODE]
Network Define:
[CODE]
function NetworkDefine(Type, VarName)
local ReadFunction
local WriteFunction
local SetFunctionName = "Set" .. VarName
local GetFunctionName = "Get" .. VarName
if Type == "Number" then
ReadFunction = net.ReadFloat
WriteFunction = net.WriteFloat
elseif Type == "Bool" then
ReadFunction = net.ReadBool
WriteFunction = net.WriteBool
GetFunctionName = "Is" .. VarName
elseif Type == "String" then
ReadFunction = net.ReadString
WriteFunction = net.WriteString
elseif Type == "Color" then
function ReadFunction()
return util.JSONToTable(net.ReadString())[1]
end
function WriteFunction(Colour)
net.WriteString(util.TableToJSON({Colour}))
end
end
return ReadFunction, WriteFunction, SetFunctionName, GetFunctionName
end
[/CODE]
Ah, I see. If you're not using this on multiple instances of objects, you could make use of `SetGlobal*` functions.
As for your code, there's still one issue and that's people spawning in after you've set the information. You're going to have to use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/PlayerInitialSpawn]GM:PlayerInitialSpawn[/url] and send the current information to them, otherwise they won't have it until you update it again.
Lastly, this isn't really a problem, but why are you sending colors as JSON strings?
[QUOTE=bigdogmat;52275992]Ah, I see. If you're not using this on multiple instances of objects, you could make use of `SetGlobal*` functions.
As for your code, there's still one issue and that's people spawning in after you've set the information. You're going to have to use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/PlayerInitialSpawn]GM:PlayerInitialSpawn[/url] and send the current information to them, otherwise they won't have it until you update it again.
Lastly, this isn't really a problem, but why are you sending colors as JSON strings?[/QUOTE]
"Lastly, this isn't really a problem, but why are you sending colors as JSON strings?" - I know this is not the best way, but as I'm testing this function so I wrote in this way.
"As for your code, there's still one issue and that's people spawning in after you've set the information." I made a way to resolve this already, but the "parameter value = ply" issue still occurring, Get Functions are returning a normal value (Number, Bool, String), but in the set functions I still receiving that Value is equal to a player.
Sorry, you need to Log In to post a reply to this thread.