Ulysses

General => Developers Corner => Topic started by: Seba on April 14, 2015, 07:50:47 AM

Title: How to display uTime in Days: Minuts: Seconds:
Post 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

Code: [Select]
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 )
Title: Re: How to display uTime in Days: Minuts: Seconds:
Post by: Buzzkill on April 14, 2015, 08:57:57 PM
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...

Code: [Select]
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.

Code: [Select]
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()...

Code: [Select]
ULib.tsay( ply, "Your playtime is ".. timeToStr( ply:GetUTimeTotalTime() )  )
Title: Re: How to display uTime in Days: Minuts: Seconds:
Post by: Seba on April 15, 2015, 09:27:41 AM
Thanks