Jump to content

Clock


Mefisto_PL

Recommended Posts

I have a problem.. I want to make timer.. When I use the command then create a count which is higher and higher and higher and when I use another command then it's stopped.

In my version of code it can't be stopped..

screenX,screenY = guiGetScreenSize() 
function startTheClock () 
  
    if not systemUpTime then 
            systemUpTime = getTickCount () --Store the system tick count, this will be 0 for us 
    end 
  
    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) 
        addEventHandler ( "onClientRender", root, startTheClock ) 
end 
addCommandHandler("clock", startTheClock) 
  

In debugscript is error that onClientRender is already handled, but when I remove this handler it doesn't create a timer.. pls help..

Link to comment
screenX,screenY = guiGetScreenSize() 
function startTheClock () 
  
    if not systemUpTime then 
            systemUpTime = getTickCount () 
    end 
  
    currentCount = getTickCount () 
  
    addEventHandler ( "onClientRender", root,  
    function() 
    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 
    ) 
end 
addCommandHandler("clock", startTheClock) 
  
startTheClock() 
  

Edited by Guest
Link to comment

to stop timer use

killTimer 

EDIT: There is nothing wrong, i think

Btw, here is an example for stopping timer:

timerDie = setTimer( function() 
outputChatBox( 'Trololo', 255, 0, 255 ) 
end, 5000, 1 
) 
  
function KillTheTimer() 
killTimer( timerDie ) 
end 
addCommandHandler('killThatTimer', KillTheTimer) 

Edited by Guest
Link to comment

I do not understand your code at all. It should always return a zero value because you're minusing it from tick count and another tick count. There's no sense at all.

This is one example how to make a timer clock.

/clock = toggle the clock ticking

/display = toggle the clock display

Client-side

local screenX, screenY = guiGetScreenSize() 
local second = 0 
local display = false 
  
addCommandHandler("clock", 
    function() 
        if isTimer(clocktimer) then 
            killTimer(clocktimer) 
        else 
            clocktimer = setTimer(function() second = second+1 end, 1000, 0) 
        end 
    end 
) 
  
addCommandHandler("display", 
    function(cmd) 
        if display then 
            removeEventHandler("onClientRender", root, displayClock) 
        else 
            addEventHandler("onClientRender", root, displayClock) 
        end 
    end 
) 
  
function displayClock() 
    dxDrawRectangle(screenX * 0.40, screenY * 0.09, 250, 50, tocolor(0,0,0,150)) 
    dxDrawText(second, screenX * 0.48, screenY * 0.1, screenX, screenY, tocolor(255,255,255), 2) 
end 

Edited by Guest
Link to comment

Forgot one thing.

Client-side

local screenX, screenY = guiGetScreenSize() 
local second = 0 
local display = false 
  
addCommandHandler("clock", 
    function() 
        if isTimer(clocktimer) then 
            killTimer(clocktimer) 
        else 
            clocktimer = setTimer(function() second = second+1 end, 1000, 0) 
        end 
    end 
) 
  
addCommandHandler("display", 
    function(cmd) 
        if display then 
            removeEventHandler("onClientRender", root, displayClock) 
            display = false 
        else 
            addEventHandler("onClientRender", root, displayClock) 
            display = true 
        end 
    end 
) 
  
function displayClock() 
    dxDrawRectangle(screenX * 0.40, screenY * 0.09, 250, 50, tocolor(0,0,0,150)) 
    dxDrawText(second, screenX * 0.48, screenY * 0.1, screenX, screenY, tocolor(255,255,255), 2) 
end 

Link to comment

/resetclock to reset the ticker.

Client-side

local screenX, screenY = guiGetScreenSize() 
local second = 0 
local display = false 
  
addCommandHandler("clock", 
    function() 
        if isTimer(clocktimer) then 
            killTimer(clocktimer) 
        else 
            clocktimer = setTimer(function() second = second+1 end, 1000, 0) 
        end 
    end 
) 
  
addCommandHandler("resetclock", 
    function() 
        second = 0 
    end 
) 
  
addCommandHandler("display", 
    function(cmd) 
        if display then 
            removeEventHandler("onClientRender", root, displayClock) 
            display = false 
        else 
            addEventHandler("onClientRender", root, displayClock) 
            display = true 
        end 
    end 
) 
  
function displayClock() 
    dxDrawRectangle(screenX * 0.40, screenY * 0.09, 250, 50, tocolor(0,0,0,150)) 
    dxDrawText(second, screenX * 0.48, screenY * 0.1, screenX, screenY, tocolor(255,255,255), 2) 
end 

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...