Jump to content

Timer - getTickCount()


FlyingSpoon

Recommended Posts

screenX,screenY = guiGetScreenSize() 
function startTheClock () 
  
    systemUpTime = 2000 --Store the system tick count, this will be 0 for us 
  
  
    currentCount = getTickCount ()  
  
    dxDrawRectangle (screenX *.40, screenY * .09, 250, 50, tocolor(0,0,0,150)) 
    dxDrawText ( currentCount - systemUpTime, screenX * .48, screenY * .1, screenX, screenY, tocolor(255,255,255), 2) 
end 
addEventHandler ( "onClientRender", root, startTheClock ) 

Link to comment

Try that and tell me the result,

screenX,screenY = guiGetScreenSize() 
function startTheClock () 
    systemUpTime = 120000   
    currentCount = getTickCount () 
    dxDrawRectangle (screenX *.40, screenY * .09, 250, 50, tocolor(0,0,0,150)) 
    dxDrawText(tostring(currentCount-systemUpTime+getTickCount()), screenX * .48, screenY * .1, screenX, screenY, tocolor(255,255,255), 2) 
end 
addEventHandler ( "onClientRender", root, startTheClock) 

btw, 2000 not equal 2minutes, it's 2secs !

Link to comment

KariiiM, your code will draw something like 199999, always :D

That is a way:

local starttime = 0 
local screenX,screenY = guiGetScreenSize() 
  
local function startTheClock ( ) 
      if getTickCount()-starttime >= 2 * 60 * 1000 then   -- if 2 min past 
           -- whatever you want to do 
      else 
           -- whatever you want to do 
      end 
end  
  
addCommandHandler ( "starttheclock", function ( ) 
     starttime = getTickCount() 
     addEventHandler ( "onClientRender", root, startTheClock ) 
end ) 

Link to comment
local endtime = 0 
  
local function renderCountdown ( ) 
     local lefttime = endtime - getTickCount()             
     if lefttime > 0 then 
          dxDrawText ( math.ceil ( lefttime ) ...  
     elseif lefttime >= -3000 then 
          dxDrawText ( "GO!" ... ) 
     else 
          removeEventHandler ( "onClientRender", root, renderCountdown ) 
     end 
end  
  
addCommandHandler ( "startcountdown", function ( _, timeinsec ) 
     endtime = getTickCount() + timeinsec * 1000         -- To save the time when the countdown will end 
     addEventHandler ( "onClientRender", root, renderCountdown ) 
end ) 

Link to comment
  
local endtime = 0 
  
local function renderCountdown ( ) 
     local lefttime = endtime - getTickCount()             
     if lefttime > 0 then 
          dxDrawText(math.ceil ( lefttime ), screenX * .48, screenY * .1, screenX, screenY, tocolor(255,255,255), 2) 
     elseif lefttime >= -3000 then 
          dxDrawText("GO!", screenX * .48, screenY * .1, screenX, screenY, tocolor(255,255,255), 2) 
     else 
          removeEventHandler ( "onClientRender", root, renderCountdown ) 
     end 
end 
  
addCommandHandler ( "test", function ( _, timeinsec ) 
     endtime = getTickCount() + timeinsec * 1000         -- To save the time when the countdown will end 
     addEventHandler ( "onClientRender", root, renderCountdown ) 
end ) 

Doesn't display my time left.

Link to comment
  
local endtime = 0 
  
local function renderCountdown ( ) 
     local lefttime = endtime - getTickCount()             
     if lefttime > 0 then 
          dxDrawText(math.ceil ( lefttime ), screenX * .48, screenY * .1, screenX, screenY, tocolor(255,255,255), 2) 
     elseif lefttime >= -3000 then 
          dxDrawText("GO!", screenX * .48, screenY * .1, screenX, screenY, tocolor(255,255,255), 2) 
     else 
          removeEventHandler ( "onClientRender", root, renderCountdown ) 
     end 
end 
  
addCommandHandler ( "timer", function ( _, timeinsec ) 
     endtime = getTickCount() + timeinsec * 1000         -- To save the time when the countdown will end 
     addEventHandler ( "onClientRender", root, renderCountdown ) 
end ) 

attempt to perform arithmetic on local 'timeinsec' (a nil value) 

Link to comment
local totalleft = endtime - getTickCount() 
local minutesleft = math.floor ( totalleft / 60000 ) 
local secondsleft = math.floor ( ( totalleft - minutsleft * 60000 ) / 1000 ) 
local time = minutesleft..":"..secondsleft >= 10 and secondsleft or "0"..secondsleft 

time is the converted time.

Should work, test it.

Link to comment
screenX,screenY = guiGetScreenSize() 
  
local function renderCountdown ( ) 
     local lefttime = endtime - getTickCount() 
     local minutesleft = math.floor ( lefttime / 60000 ) 
     local secondsleft = math.floor ( ( lefttime - minutesleft * 60000 ) / 1000 ) 
     local timet = minutesleft..":"..secondsleft >= 10 and secondsleft or "0"..secondsleft          
     if lefttime > 0 then 
          dxDrawText(math.ceil ( timet ), screenX * .48, screenY * .1, screenX, screenY, tocolor(255,255,255), 2) 
     elseif lefttime >= -3000 then 
          dxDrawText("GO!", screenX * .48, screenY * .1, screenX, screenY, tocolor(255,255,255), 2) 
     else 
          removeEventHandler ( "onClientRender", root, renderCountdown ) 
     end 
end 
  
addCommandHandler ( "timer", function ( _, timeinsec ) 
     endtime = getTickCount() + tonumber ( timeinsec  ) * 1000 
     addEventHandler ( "onClientRender", root, renderCountdown ) 
end ) 

I did this but it don't work.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...