a script that does not show the normal time but the game time
Hours are minutes and seconds are minutes
Thanks
What do you exactly mean by "game time" and "normal time"?
I think he's saying instead of 1 hour being an actual hour that 1 hour is actually 1 minute
Yes
[lua]
--"game time" clock
local thetime = RealTime()
local seconds = math.floor((thetime * 60) % 60) --seconds are "fake"
local minutes = math.floor(thetime % 60) --minutes are the new seconds
local hours = math.floor((thetime / 60) % 60) --hours are the new minutes
local days = math.floor(thetime / 3600) --days are the new hours
[/lua]
Should give you what you're looking for.
Thanks is this the same of all the players? when i create a hud?
[QUOTE=devilkill;34266933]Thanks is this the same of all the players? when i create a hud?[/QUOTE]
For a consistent time across all players, you should declare those variables on the server and then either network them or send them over to the player every tick or so to update the clocks on their HUDs.
Mentioning that, please don't forget that you need to re-set these variables every tick or so, or they won't update.
can you give me a example
<snip>
Terrible example. Reposted below.
Why not use a Think hook, then use a variable set to CurTime()
[lua]
local tick = CurTime()
hook.Add("Think","Custom.UpdateTime",function()
if tick + 1 < CurTime() then
for k,v in pairs(player.GetAll()) do
v:SendLua("")
end
end
end)
[/lua]
[QUOTE=skullorz;34282033]Why not use a Think hook, then use a variable set to CurTime()
[lua]
local tick = CurTime()
hook.Add("Think","Custom.UpdateTime",function()
if tick + 1 < CurTime() then
for k,v in pairs(player.GetAll()) do
v:SendLua("")
end
end
end)
[/lua][/QUOTE]
Oh yeah, I forgot that I could loop through players like that. Oh haha, that was just stupid of me.
I used Tick because it's uniform to client and server and because it's only called when there are users connected. There's no point in running the function if there's no one to benefit from it.
I used RealTime because it's a more concrete time. When used on the server it is the time since it has been turned on. CurTime can't be used for a clock because it only runs when players are present, and that brings us to the "if a tree falls in a forest and no one is around to hear it, does it make a sound" issue. Time is forever. I could have probably used SysTime as a substitute. Less large a number (especially on servers that basically never shut down).
Thanks guys but how can i buld the hud
[QUOTE=devilkill;34289281]Thanks guys but how can i buld the hud[/QUOTE]
If you want someone to build you a HUD with the game-time built into it, we'll need more detail than that. If you honestly don't know how to make a HUD and just want to know that, here are some links:
[B]HUD Tutorials:[/B]
[url]http://wiki.garrysmod.com/?title=Creating_a_HUD[/url]
[url]http://wiki.garrysmod.com/?title=LUA:Hud_Tutorial[/url]
[url]http://wiki.garrysmod.com/?title=How_to:_HUD[/url] (Useful mostly for its HUD element-hiding setup, very clean)
[B]Useful Libraries:[/B]
[url]http://wiki.garrysmod.com/?title=Surface[/url]
[url]http://wiki.garrysmod.com/?title=Draw[/url]
[editline]19th January 2012[/editline]
Also, redid the example with usermessages and much cleaner code.
To use, serverside in lua/autorun/server/, the other in lua/autorun/client/.
[B]sv_gameclock.lua[/B]
[lua]
local thetime = {}
thetime.base = 0
thetime.seconds = 0
thetime.minutes = 0
thetime.hours = 0
thetime.days = 0
local function update_time()
thetime.base = SysTime()
thetime.seconds = math.floor((thetime.base * 60) % 60)
thetime.minutes = math.floor(thetime.base % 60)
thetime.hours = math.floor((thetime.base / 60) % 60)
thetime.days = math.floor(thetime.base / 3600)
umsg.Start("gameclock")
umsg.Short(thetime.seconds)
umsg.Short(thetime.minutes)
umsg.Short(thetime.hours)
umsg.Short(thetime.days)
umsg.End()
end
hook.Add("Tick", "gameclocktick", update_time)
[/lua]
[B]cl_gameclock.lua[/B]
[lua]
local cltime = {}
cltime.seconds = 0
cltime.minutes = 0
cltime.hours = 0
cltime.days = 0
usermessage.Hook("gameclock", function(msgdata)
cltime.seconds = msgdata:ReadShort()
cltime.minutes = msgdata:ReadShort()
cltime.hours = msgdata:ReadShort()
cltime.days = msgdata:ReadShort()
end)
local function draw_HUD()
draw.RoundedBox(1, ScrW() - 210, 10, 200, 25, Color(0, 0, 0, 150))
draw.DrawText("Day: "..cltime.days.." - ".."Time: "..cltime.hours..":"..cltime.minutes..":"..cltime.seconds, "ChatFont", ScrW() - 195, 15, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT)
end
hook.Add("HUDPaint", "gameclockHUD", draw_HUD)
[/lua]
Error:
Warning: Unhandled usermessage 'gameclock'
[QUOTE=devilkill;34292322]Error:
Warning: Unhandled usermessage 'gameclock'[/QUOTE]
Works fine when I use it. Make sure you're running it in Multiplayer.
[QUOTE=wizardsbane;34292529]Works fine when I use it. Make sure you're running it in Multiplayer.[/QUOTE]
You never AddCSLuaFile'd cl_gameclock
[QUOTE=zzaacckk;34293203]You never AddCSLuaFile'd cl_gameclock[/QUOTE]
You shouldn't need to when running it locally (Start New Game -> Multiplayer). That was just an example, not something to use on an actual server D:
I uploadt on my server
Can you mabye helpe me to create it for a server?
[QUOTE=devilkill;34293539]I uploadt on my server
Can you mabye helpe me to create it for a server?[/QUOTE]
See, that's it. If you use that on a server, add this to the top of the serverside file:
[lua]AddCSLuaFile("autorun/client/cl_gameclock.lua")[/lua]
Is that all you wanted? Or did you have something bigger in mind?
[lua]
AddCSLuaFile("autorun/client/cl_gameclock.lua")
local thetime = {}
thetime.base = 0
thetime.seconds = 0
thetime.minutes = 0
thetime.hours = 0
thetime.days = 0
local function update_time()
thetime.base = SysTime()
thetime.seconds = math.floor((thetime.base * 60) % 60)
thetime.minutes = math.floor(thetime.base % 60)
thetime.hours = math.floor((thetime.base / 60) % 60)
thetime.days = math.floor(thetime.base / 3600)
umsg.Start("gameclock")
umsg.Short(thetime.seconds)
umsg.Short(thetime.minutes)
umsg.Short(thetime.hours)
umsg.Short(thetime.days)
umsg.End()
end
hook.Add("Tick", "gameclocktick", update_time)
[/lua]
Now it wil works i wil test it and Wizardsbane can i mabey add you on steam?
[QUOTE=devilkill;34293748]Now it wil works i wil test it and Wizardsbane can i mabey add you on steam?[/QUOTE]
Sure, no problem.
I can't add you
[QUOTE=devilkill;34293905]I can't add you[/QUOTE]
Someone else from here added me the other day. Why can't you?
[editline] January 19, 2012 [/editline]
I added you instead.
The problem is now with the time script if the game hours is 24 it gows to 25 and not to day 1 and hours 0 pleas help
In [B]sv_gamelock.lua[/B]:
[lua]
local thetime = {}
thetime.base = 0
thetime.seconds = 0
thetime.minutes = 0
thetime.hours = 0
thetime.days = 0
local function update_time()
thetime.base = SysTime()
thetime.seconds = math.floor((thetime.base * 60) % 60)
thetime.minutes = math.floor(thetime.base % 60)
thetime.hours = math.floor((thetime.base / 60) % 60)
thetime.days = math.floor(thetime.base / 3600)
if thetime.hours - 24 >= 1 then thetime.hours = thetime.hours-(24*math.floor(thetime.hours/24)) end
umsg.Start("gameclock")
umsg.Short(thetime.seconds)
umsg.Short(thetime.minutes)
umsg.Short(thetime.hours)
umsg.Short(thetime.days)
umsg.End()
end
hook.Add("Tick", "gameclocktick", update_time)
[/lua]
It's untested, just tell me if it works.
Oke i wil test it now
[editline]20th January 2012[/editline]
Its not working its 25 now
[editline]20th January 2012[/editline]
and its gowing to 0 but the day is not + 1
Fixed: [url]http://pastie.org/private/stkzdkjglrtuy5lxiw367g[/url]
I hope. :suicide:
It works you are the best thanks
Sorry, you need to Log In to post a reply to this thread.