I'm trying to make a gamemode and add a timer. I got it to appear, but the thing is it isn't synced to the server. I put the timer code in shared.lua and the HUD code in cl_init.lua.
[code]
function GameTimer()
STimer = 0
MTimer = 0
timer.Create("DoorTimer",1,0,function() STimer = STimer + 1 if STimer == 60 then STimer = 0 MTimer = MTimer + 1 end end)
end
[/code]
[code]
function GM:HUDPaint()
self:PaintWorldTips()
-- Draw all of the default stuff
BaseClass.HUDPaint( self )
self:PaintNotes()
local ply = LocalPlayer()
surface.CreateFont( "MyFont", {
font = "Arial",
size = 28,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = false,
} )
surface.SetTextColor(255, 255, 255, 255)
surface.SetTextPos( 20,20 )
surface.SetFont( "MyFont" )
surface.DrawText(tostring(MTimer)..":"..tostring(STimer))
end
[/code]
For one, remove your surface.CreateFont code from HUDPaint. It's called every frame; you'll eventually run out of memory and crash. Not related to your particular issue, but it's still a problem.
Definitely do what crazyscouter says, and adding to that: here's an example of a timer which syncs to the client.
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/sh_hud_countdown.lua.html[/url]
timer.TimeLeft can be used to accomplish the same thing though ( with networking it ), but knowing how to calculate time remaining vs time-elapsed is simple, but good to know.
[QUOTE=Acecool;45982919]Definitely do what crazyscouter says, and adding to that: here's an example of a timer which syncs to the client.
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/sh_hud_countdown.lua.html[/url]
timer.TimeLeft can be used to accomplish the same thing though ( with networking it ), but knowing how to calculate time remaining vs time-elapsed is simple, but good to know.[/QUOTE]
The guide was helpful and i almost got it, but my timer is supposed to count up. Any idea how to do that?
Use the time elapsed calculation instead of time-remaining.
[QUOTE=Acecool;45992184]Use the time elapsed calculation instead of time-remaining.[/QUOTE]
I got it to work and it's well synced, but when I connect two clients their times are off by around 10-20 seconds. Not really a huge problem for me, but it would be helpful to fix it.
CurTime should be synced between all clients... The time should send the starting time that was captured from server CurTime to all clients, so all clients should get a start time and the duration, then process the calculations by themselves...
I'll double check / look into it. But, what you could try is replace all CurTime with RealTime, etc...
Got it working thanks for the help!
Sorry, you need to Log In to post a reply to this thread.