Jump to content

Sleep() function


Tonyx97

Recommended Posts

Hi all, I need a "timer" with less than 50 miliseconds, I remember the function sleep() for Lua, but doesn't work in MTA, I use getTickCounts but I don't know how to use, see the code:

function callTimer ( key, keyState ) 
    if keyState == "down" then 
        pos = 0 
        sleep = 1500 
        lastCount = getTickCount() 
        while getTickCount() >= lastCount+sleep then 
            if pos >= 100 then 
                pos = 0 
                outputChatBox("test") 
                unbindKey("n","both",callTimer) 
                break 
            end 
            pos = pos + 1 
            outputChatBox(pos) 
            lastCount = getTickCount() 
        end 
    elseif keyState == "up" then 
        if pos < 100 then 
            pos = 100 
        end 
    end 
end 
bindKey("num_5","both",callTimer) 

I want that print the number every 1.5 seconds while I'm pressing the num_5

Link to comment

I dont know what you want to achieve exactly with that code, but I guess you are trying to do something with animations, so you should use onClientRender and getKeyState (and/or bindKey and save the key state) instead in that case.

onClientRender is called each frame, which means if you have 50 fps, then it will run each 1000/50 = 20ms.

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