In-Game Clock?

Hi Facepunchers! (That’s a funny picture…)

I was wondering, how would I go on creating an in-game Clock?

Like, when the server starts, the clock’s supposed to be at 12:00pm, and then increase every minute, 12:01pm, like a normal clock.

It should not have anything to do with the client’s computer time, or anything like that, just a random timer on the server, which everyone shares.

  • How would I go on drawing it in the right corner of the screen?

Thanks in advance!

  • Busymonkey

There is a tutorial on how to do that.

I did something like that except every minute was a hour and second was a minute, for my day and night thing :stuck_out_tongue_winking_eye:

But yeah if you want it normal follow that tutorial (assuming it does what you want).

No, that’s not it, unfortuneately.
You see, that clock gets the value of time and date from your computer,
and that’s not what I want :o

Thanks anyways though :slight_smile:

Something like this?

[lua]starttime = os.clock()

function os.ServerTime()
return string.ToHourMinute(CurTime() - starttime)
end

function string.ToHourMinute(sec)
local hours = sec % (60 * 60)
local minutes = sec % 60
if string.len(hours) < 2 then
hours = “0”…hours
end
if string.len(minutes) < 2 then
minutes = “0”…minutes
end
return hours…":"…minutes
end
[/lua]

Uhm, I guess so, lol :smiley:

How would I got on drawing this in a hud?

bump

How can I draw the given code, into the right corner of the screen?

You can use the draw library or the surface library to draw the text and the box in a HUDPaint hook.

**[Draw

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

**[Surface

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

**[Gamemode.HUDPaint

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

I got this from the wiki:


if (SERVER) then
	AddCSLuaFile("autorun/clock.lua")
end
 
if (CLIENT) then
 
	function clockhud()
		draw.RoundedBox( 0, ScrW()*0.01, ScrH()*0.01, 128, 46, Color( 25, 25, 25, 255 ) )
		draw.RoundedBox( 20, ScrW()*0.01, ScrH()*0.01, 128, 46, Color( 125, 125, 125, 125 ) )
		draw.SimpleText(os.date( "%a, %I:%M:%S %p" ), "Default", ScrW()*0.024, ScrH()*0.02, Color( 255, 104, 86, 255 ),0,0)
		draw.SimpleText(os.date( "%m/%d/20%y" ), "Default", ScrW()*0.035, ScrH()*0.04, Color( 255, 104, 86, 255 ),0,0)
	end
 
	hook.Add("HUDPaint", "clockhud", clockhud)
end


What could I use in the code FlapJack gave me, instead of the

draw.SimpleText(os.date( “%m/%d/20%y” )?

I think my code is wrong, but that’s the general idea. And yeah.

What’s wrong about it, and what “And yeah.”? :o

You could use my code in it, assuming it’s right. If not, edit it so as it is. I’m too busy to check right now.

[lua]function string.ToHourMinutes(sec)
local hours = math.floor(sec/3600)
local minites = (sec%3600)/60
if #tostring(hours) < 2 then
hours = “0”…hours
end
if #tostring(minutes) < 2 then
minutes = “0”…minutes
end
return hours…":"…minutes
end[/lua]

To be honest, this isn’t the best way to do it.

[editline]09:07PM[/editline]

You need to send periodic updates to the client letting them know the correct time and let the clients interpolate the time in between.

You need to send periodic updates to the client letting them know the correct time and let the clients interpolate the time in between.
[/QUOTE]

Or at least send them the RealTime() value of the server the moment they connect.

You only really need to send it once, after that the client should be able to predict it to a good degree.

The clients time will slowly deviate from the servers if you only send it once. In my experience, I needed to send an update every so often to set it back to the correct time.

[editline]09:15PM[/editline]

Damn edit |FlapJack|

[editline]09:16PM[/editline]

Another problem is that when the user opens the menu or the console, the time on the client will stop. So if the client has the console open for 20 seconds, the clock will be 20 seconds behind server time and will need to be updated.

As far as I know, os.clock is independent of hooks (Therefore will still be updated even if hooks aren’t running)

You are right, I was talking about a different method of getting the time (using a timer to increment a variable on the client every second and updating it every so often from the server).

Sorry to break your little conversation, but I’m not that experienced with Lua scripting yet, so could you please point me in direction again?

Well, obviously you would calculate the offset with the server´s os.time when you receive it.