GetTickCount: Difference between revisions
mNo edit summary |
(Improve example.) |
||
(6 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
{{Server client function}} | |||
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. | 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. | ||
{{Server client function}}{{Important Note|The value returned by this function client-side may not be reliable if a client is maliciously modifying their operating system speed.}} | |||
==Syntax== | ==Syntax== | ||
Line 9: | Line 12: | ||
==Example== | ==Example== | ||
The below code is a good example on how to implement basic anti-spam protection for a command, for which using getTickCount is better than the regular timer functions. | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
local spam = {} | |||
function setSkin(player, cmd, skin) | |||
if spam[player] and getTickCount() - spam[player] < 4000 then | |||
return outputChatBox("You cannot change skin that often!", player, 255, 0, 0) | |||
end | |||
skin = skin and tonumber(skin) | |||
if getElementModel(player) == skin or isPedDead(player) then | |||
return | |||
end | |||
if skin and skin <= 99999 then -- what do we know about dynamic ped ID range? | |||
setElementModel(player, skin) | |||
spam[player] = getTickCount() | |||
else | |||
outputChatBox("Invalid skin ID!", player, 255, 0, 0) | |||
end | |||
end | |||
addCommandHandler("skin", setSkin) | |||
function cleanUp() | |||
if spam[source] then | |||
spam[source] = nil | |||
end | |||
end | |||
addEventHandler("onPlayerQuit", root, cleanUp) | |||
</syntaxhighlight> | |||
This example will start a timer displayed at the top of the screen displayed every frame. You can log any specific moment in time by outputting the currentCount variable. | |||
-- | <syntaxhighlight lang="lua"> | ||
local firstColor = tocolor(0, 0, 0, 150) | |||
local secondColor = tocolor(255, 255, 255, 255) | |||
local screenX, screenY = guiGetScreenSize() | |||
local currentCount = 0 | |||
local systemUpTime = getTickCount() -- Store the system tick count, this will be 0 for us | |||
function onClientRenderClock() | |||
currentCount = getTickCount() | |||
dxDrawRectangle(screenX *.40, screenY * .09, 250, 50, firstColor) | |||
dxDrawText(currentCount - systemUpTime, screenX * .48, screenY * .1, screenX, screenY, secondColor, 2) | |||
end | |||
addEventHandler("onClientRender", root, onClientRenderClock) | |||
</syntaxhighlight> | |||
==See Also== | ==See Also== | ||
{{Utility functions}} | {{Utility functions}} | ||
[[ru:GetTickCount]] |
Latest revision as of 11:54, 12 September 2021
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.
Important Note: The value returned by this function client-side may not be reliable if a client is maliciously modifying their operating system speed. |
Syntax
int getTickCount ()
Returns
Returns an integer containing the number of milliseconds since the system the server is running on started. This has the potential to wrap-around.
Example
The below code is a good example on how to implement basic anti-spam protection for a command, for which using getTickCount is better than the regular timer functions.
local spam = {} function setSkin(player, cmd, skin) if spam[player] and getTickCount() - spam[player] < 4000 then return outputChatBox("You cannot change skin that often!", player, 255, 0, 0) end skin = skin and tonumber(skin) if getElementModel(player) == skin or isPedDead(player) then return end if skin and skin <= 99999 then -- what do we know about dynamic ped ID range? setElementModel(player, skin) spam[player] = getTickCount() else outputChatBox("Invalid skin ID!", player, 255, 0, 0) end end addCommandHandler("skin", setSkin) function cleanUp() if spam[source] then spam[source] = nil end end addEventHandler("onPlayerQuit", root, cleanUp)
This example will start a timer displayed at the top of the screen displayed every frame. You can log any specific moment in time by outputting the currentCount variable.
local firstColor = tocolor(0, 0, 0, 150) local secondColor = tocolor(255, 255, 255, 255) local screenX, screenY = guiGetScreenSize() local currentCount = 0 local systemUpTime = getTickCount() -- Store the system tick count, this will be 0 for us function onClientRenderClock() currentCount = getTickCount() dxDrawRectangle(screenX *.40, screenY * .09, 250, 50, firstColor) dxDrawText(currentCount - systemUpTime, screenX * .48, screenY * .1, screenX, screenY, secondColor, 2) end addEventHandler("onClientRender", root, onClientRenderClock)
See Also
- addDebugHook
- base64Decode
- base64Encode
- debugSleep
- decodeString
- encodeString
- fromJSON
- generateKeyPair
- getColorFromString
- getDevelopmentMode
- getDistanceBetweenPoints2D
- getDistanceBetweenPoints3D
- getEasingValue
- getNetworkStats
- getNetworkUsageData
- getPerformanceStats
- getRealTime
- getTickCount
- getTimerDetails
- getTimers
- getFPSLimit
- getUserdataType
- getVersion
- gettok
- isTransferBoxVisible
- setTransferBoxVisible
- hash
- inspect
- interpolateBetween
- iprint
- isOOPEnabled
- isTimer
- killTimer
- md5
- passwordHash
- passwordVerify
- pregFind
- pregMatch
- pregReplace
- removeDebugHook
- resetTimer
- setDevelopmentMode
- setFPSLimit
- setTimer
- ref
- deref
- sha256
- split
- teaDecode
- teaEncode
- toJSON
- tocolor
- getProcessMemoryStats
- utfChar
- utfCode
- utfLen
- utfSeek
- utfSub