Ulysses
General => Developers Corner => Topic started by: Seba on April 14, 2015, 07:50:47 AM
-
How do i display the game time on my server like this Eksample: 3 days 20 min 59 sec
DisplayUtime = { "!playtime", "/playtime" }
--- Displays you play time in chat
local function DisplayOpenUtime( ply, command, team )
for _,v in pairs(DisplayUtime) do
if command == v then
ULib.tsay( ply, "Your playtime is ".. math.floor( ply:GetUTimeTotalTime() ) .. " (This is in seconds, trying to fix it!) " )
end
end
end
hook.Add( "PlayerSay", "DisplayUtime", DisplayOpenUtime )
-
If you look in cl_utime.lua, you'll see how the HUD ui (which includes the same kind of string you're looking for) is constructed...
self.total:SetText( timeToStr( LocalPlayer():GetUTimeTotalTime() ) )
You're already working with ply:GetUTimeTotalTime(), so your only missing piece (the formatting) is actually contained in that timeToStr() call. timeToStr() is a helper function declared in sh_utime.lua which converts the player utime into a w:d:h:m:s style string.
function timeToStr( time )
local tmp = time
local s = tmp % 60
tmp = math.floor( tmp / 60 )
local m = tmp % 60
tmp = math.floor( tmp / 60 )
local h = tmp % 24
tmp = math.floor( tmp / 24 )
local d = tmp % 7
local w = math.floor( tmp / 7 )
return string.format( "%02iw %id %02ih %02im %02is", w, d, h, m, s )
end
so all you should need to do is take your original code (without the math.floor) and call timeToStr()...
ULib.tsay( ply, "Your playtime is ".. timeToStr( ply:GetUTimeTotalTime() ) )
-
Thanks