• Client,Server,Shared
    8 replies, posted
From what I've read here : [url]http://wiki.garrysmod.com/?title=Networking_Variables[/url] If you change variable on the server, they should change clientside, but thats not working. here is the code I'm using (the relevant code) : shared : [lua] cycleTime = 30 timeNextCycle = cycleTime [/lua] serverside: [lua] function GM:Think() if(timeNextCycle <= 0) then timeNextCycle = cycleTime; end timeNextCycle = timeNextCycle - FrameTime() end [/lua] clientside: [lua] function GM:HUDPaint( ply ) draw.WordBox(8,10,10,"Time till next cycle: "..timeNextCycle,"ScoreboardText", Color(50,50,75,100), Color(255,255,255,255)) end [/lua] timeNextCycle remains 30 clientside.
[QUOTE=quincy18;26799865]From what I've heard if you change variable on the server, they should change clientside.[/QUOTE]Thats true, but you're not networking them. See these: [B][URL="http://wiki.garrysmod.com/?title=Entity.SetNetworkedInt"]Entity.SetNetworkedInt [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG][/URL][/B], [B][URL="http://wiki.garrysmod.com/?title=Entity.GetNetworkedInt"]Entity.GetNetworkedInt [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG][/URL][/B].
[QUOTE=freemmaann;26800596]Thats true, but you're not networking them. See these: [B][URL="http://wiki.garrysmod.com/?title=Entity.SetNetworkedInt"]Entity.SetNetworkedInt [img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb][/URL][/B], [B][URL="http://wiki.garrysmod.com/?title=Entity.GetNetworkedInt"]Entity.GetNetworkedInt [img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb][/URL][/B].[/QUOTE] Why are you changing my post reply, I know that works, but I mentioned the article and it says that if you make a variable inside shared then changing it serverside it would update clientside, but not the other way around. which obviously doesn't work.
[QUOTE=quincy18;26800679]Why are you changing my post reply, I know that works, but I mentioned the article and it says that if you make a variable inside shared then changing it serverside it would update clientside, but not the other way around. which obviously doesn't work.[/QUOTE]Either the tutorial was wrong, or it meant to say when using networkedvar(), making a simple shared var, then changing it server side DOES NOT change it clientside.
So if I where to send this variable to all players I'd have to update this variable on each player on each think ?
There are mutiple ways like usermessage's, networkedvar's. Use networkedvars to send from server to every player and with usermessage's you can send for specific players.
A variable won't be networked unless you explicitly send its value to the client using the methods freeman mentioned. Here's a snippet of what you could try - it's not tested but it will hopefully give you an idea of what would work. On the server: [lua] function GM:Think() if timeNextCycle <= 0 then --the cycle ended timeNextCycle = cycleTime --tell all the clients a cycle just finished using a usermessage local rp = RecipientFilter() rp:AddAllPlayers() umsg.Start("CycleEnd", rp) --you can put data in a usermessage here --but we're just telling the client something happened --so that's not necessary umsg.End() end timeNextCycle = timeNextCycle - FrameTime() end [/lua] On the client: [lua] function GM:Think() timeNextCycle = timeNextCycle - FrameTime() end local function getCycleEndMsg(umsg) --if there's data in the usermessage you can read it off in this function --but there is none --the server told us a cycle ended so let's reset timeNextCycle timeNextCycle = cycleTime end usermessage.Hook("CycleEnd", getCycleEndMsg) --your GM:Paint function [/lua]
I currently have this : [lua] function GM:PlayerInitialSpawn( ply ) ply:SetTeam(1) ply:SetNWInt("period",currentPeriod) ply:SetNWInt("time",math.Round(timeNextCycle)) end function GM:Think() if(timeNextCycle <= 0) then timeNextCycle = cycleTime currentPeriod = currentPeriod + 1; if(currentPeriod == 4) then currentPeriod = 0 end end timeNextCycle = timeNextCycle - FrameTime() for k, v in pairs(player.GetAll()) do v:SetNWInt("period",currentPeriod) v:SetNWInt("time",math.Round(timeNextCycle)) end end [/lua] well I had that before I read the tutorial, is it better to do it like this or is it better to send usermessages containing those 2 variables. also doesn't frametime on clientside give the time for the screen to render, and frametime on server would be the time between each think. Wouldn't that give desync problems.
You're setting 2 networked variables every frame. Every time you set a networked variable on the server it sends the new value to all players. Which will put a lot more load on the network than is at all necessary. For example, there is no reason to change the period networked variable unless the period changed. I'm not sure about the details of FrameTime. If you're worried about desync problems you could try reworking the logic to use CurTime(). eg Server [lua] cycleTime = 30 nextCycleEnd = CurTime()+cycleTime function GM:Think() if nextCycleEnd < CurTime() then --tell everyone that the next cycle time just changed umsg.Start("NxtCyclEnd", RecipientFilter():AddAllPlayers()) umsg.Long(nextCycleEnd) umsg.End() nextCycleEnd = CurTime()+cycletime end end --we need to tell a newly joining player when the cycle ends function GM:PlayerInitialSpawn(ply) umsg.Start("NxtCyclEnd", ply) umsg.Long(nextCycleEnd) umsg.End() end [/lua] Client [lua] cycleTime = 30 nextCycleEnd = CurTime()+cycleTime local function getCycleEndMsg(umsg) nextCycleEnd = umsg:ReadLong() end usermessage.Hook("NxtCyclEnd", getCycleEndMsg) [/lua] In this case, nextCycleEnd is used to store what time the cycle will end. That will stay the same while CurTime() continually increases, eventually becoming larger than nextCycleEnd. When this happens, you just reset nextCycleEnd to be 30 seconds in the future. You can get the time until the next cycle by using nextCycleEnd-CurTime().
Sorry, you need to Log In to post a reply to this thread.