Jump to content

getTickCount


WhoAmI

Recommended Posts

This function returns amount of time that your system has been running in milliseconds. By comparing two values of getTickCount, you can determine how much time has passed (in milliseconds) between two events. This could be used to determine how efficient your code is, or to time how long a player takes to complete a task. #Wiki

Link to comment

As explained by the Wiki site:

This function returns amount of time that your system has been running in milliseconds. By comparing two values of getTickCount, you can determine how much time has passed (in milliseconds) between two events. This could be used to determine how efficient your code is, or to time how long a player takes to complete a task.

I posted an example for an issue someone was having, and it's using getTickCount.

local final_wait_time = 5000 -- Amount of ticks the last message should be displayed for 
local phrases = { 
    { 0, "The sun is shining right now" }, 
    { 5000, "And so I think it'll be a great day today" } 
} 
  
local visible, tick = false 
local function toggleRendering( ) 
    tick = getTickCount( ) 
    local fn = ( visible and removeEventHandler or addEventHandler ) 
    fn( "onClientRender", root, renderDisplay ) 
    visible = not visible 
end 
  
local screen_width, screen_height = guiGetScreenSize( ) 
function renderDisplay( ) 
    if ( not visible ) or ( not phrases ) or ( #phrases == 0 ) then return end 
    for index,data in ipairs( phrases ) do 
        if ( getTickCount( ) - tick >= data[ 1 ] ) and ( not phrases[ index + 1 ] and ( getTickCount( ) - tick < phrases[ index ][ 1 ] + final_wait_time ) or ( phrases[ index + 1 ] and getTickCount( ) - tick < phrases[ index + 1 ][ 1 ] ) ) then 
            local text_width, text_height = dxGetTextWidth( data[ 2 ] ), dxGetFontHeight( ) 
            dxDrawRectangle( ( screen_width - text_width ) / 2 - 15, ( screen_height - text_height ) / 2 - 15, text_width + 30, text_height + 30, tocolor( 0, 0, 0, 200 ) ) 
            dxDrawText( data[ 2 ], ( screen_width - text_width ) / 2, ( screen_height - text_height ) / 2, ( screen_width - text_width ) / 2 + text_width, ( screen_height - text_height ) / 2 + text_height, tocolor( 255, 255, 255, 255 ) ) 
        end 
    end 
end 
  
addEventHandler( "onClientResourceStart", resourceRoot, 
    function( ) 
        toggleRendering( ) 
    end 
) 

It basically displays text phrases, and each one has a small delay calculated with getTickCount.

So, to make it a little bit clearer:

addEventHandler( "onClientResourceStart", resourceRoot, 
    function( ) 
        local tick = getTickCount( ) 
        addEventHandler( "onClientRender", root, 
            function( ) 
                -- If 5000 ms (5 seconds) has passed since resource was loaded for the client, then... 
                if ( getTickCount( ) - tick >= 5000 ) then 
                    -- ... do something 
                end 
            end 
        ) 
    end 
) 

Edited by Guest
Link to comment

Here is another example. It will display your FPS every second on the chat.

  
local frames = 0 
local time = false 
  
function startFrames() 
    time = getTickCount() 
    addEventHandler("onClientRender",root,countFrames) 
end 
  
function countFrames() 
    if getTickCount()-time>=1000 then -- if a second passed from the last check 
        outputChatBox(tostring(frames)) -- output FPS 
        time = getTickCount() -- set the time to getTickCount() 
        frames = 0 -- reset frames 
    end 
    frames = frames+1 -- increment frames every onClientRender until 1 second is reached 
end 
  
startFrames() 
  

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