Timer on HUD? Noob LUA question.

Hey,

because there is no public “climb” gamemode for garrysmod, I want to try to make my own.

The thing I need now is a timer that resets on spawn and print the time on the hud, like a stopwatch.

I already googled but didnt find anything that helps me.

I guess its pretty easy for someone that knows lua.

thanks.

Sorry for my bad english.

**[Gamemode.PlayerSpawn

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.PlayerSpawn)**
**[G.CurTime

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=G.CurTime)**

**[Usermessage.Hook

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Usermessage.Hook)**
**[Umsg.Start

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Umsg.Start)**

[lua]
// SERVERSIDE

local function SendLifeTime( pl )

if pl.SpawnTime then -- If the varible SpawnTime is defined on the players meta table.
		
	local time = pl.SpawnTime - CurTime() -- The time between the spawn and death ( or respawn ).
		
	umsg.Start( "send_time", pl ) -- Start the usermessage send_time.
	umsg.Short( time ) -- Add the time to your usermessage with a short.
	umsg.End() -- Send it(?)
			
end
		
pl.SpawnTime = CurTime() -- Define the players spawn time with the current time.

end
hook.Add( “PlayerSpawn”, “send_life_time”, SendLifeTime )

// CLIENTSIDE
local function GetTime( um )

local time = um:ReadShort() -- Read the short that we defined with the time from the usermessage.
LocalPlayer().Time = time -- Define the varible Time on the localplayers meta table.

end
usermessage.Hook( “send_time”, GetTime ) – Hook the function to be called whenever the server sends the usermessage.
[/lua]

Now you’ll have to create a hud that prints out the value we defined to the player. ( LocalPlayer().Time )
**[Gamemode.HUDPaint

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.HUDPaint)**

I might be wrong somewhere in the comments or I might have missed something in the code, but I can not test the code at the moment.

alright, correct me if im wrong, i copied the “clientside” stuff into the “cl_init.lua” and the serverside stuff into “init.lua”, then i added my code on the client side, now it looks like this:
[lua]
local function GetTime( um )

local time = um:ReadShort() -- Read the short that we defined with the time from the usermessage.
LocalPlayer().Time = time -- Define the varible Time on the localplayers meta table.
surface.CreateFont ("coolvetica", ScreenScale(30), 400, true, false, "HUDFONT")
surface.SetTextColor( 255, 255, 255, 255 )
surface.SetTextPos(34, (ScrH()/2)+(ScrH()/4) )
surface.SetFont("HUDFONT")
surface.DrawText( LocalPlayer().Time )

end
usermessage.Hook( “send_time”, GetTime ) – Hook the function to be called whenever the server sends the usermessage.
[/lua]

and it isnt working, isnt giving me any errors, but just isnt showing up on hud.

You need to draw it in a render hook, for example **[Gamemode.HUDPaint

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.HUDPaint)**.
[lua]
local function TimeHud()

-- Do your drawing in here.

end
hook.Add( “HUDPaint”, “draw_time”, TimeHud )
[/lua]

Hm, i dont know much about hooks, so i just need to do:
[lua]
local function TimeHud()

surface.CreateFont ("coolvetica", ScreenScale(30), 400, true, false, "HUDFONT")
surface.SetTextColor( 255, 255, 255, 255 )
surface.SetTextPos(34, (ScrH()/12)+(ScrH()/14) )
surface.SetFont("HUDFONT")
surface.DrawText( LocalPlayer().Time )

end
hook.Add( “HUDPaint”, “draw_time”, TimeHud )
[/lua]
I’m sorry if im wrong again…
if i do it like that he prints me numbers like “-16” or “-8” totaly random. The value only updates on respawn.

[editline]13th October 2011[/editline]

alright, i got this thing working, now my only problem is, the time only updates on respawn. But i need like smooth running timer. so a way to update every second would be awesome.

Ah, sorry about that. It should be [lua]local time = CurTime() - pl.SpawnTime[/lua]
but I believe you figured that out yourself.

Then we just use the server to tell the client that the timer should restart.

[lua]
// SERVERSIDE
local function RestartTime( pl )

umsg.Start( "restart_time", pl ) -- We send no data in this since we just want to tell the player to restart the timer.
umsg.End()

end
hook.Add( “PlayerSpawn”, “restart_time”, RestartTime )

// CLIENTSIDE
local function RestartTime()

LocalPlayer().Time = CurTime()

end
usermessage.Hook( “restart_time”, RestartTime )

surface.CreateFont( “coolvetica”, ScreenScale(30), 400, true, false, “HUDFONT” ) – You don’t have to create it on every frame.
local function TimeHud()

local time = CurTime() - LocalPlayer().Time -- The current time minus the time the player got when the usermessage was sent.
	
surface.SetTextColor( 255, 255, 255, 255 )
surface.SetTextPos( 34, ( ScrH() / 12 ) + ( ScrH() / 14 ) )
surface.SetFont( "HUDFONT" )
surface.DrawText( time )

end
hook.Add( “HUDPaint”, “draw_time”, TimeHud )
[/lua]

if you don’t understand what Thermadyle just posted, you’re basically gonna have to set the time you want to start counting, in this case have the usermessage trigger that, then do the calculation of ‘current time - start time’ to get the time that has passed since you started counting.

Also, you can use **[Math.floor

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Math.floor)** to turn it into a neat round number.