CreateCountTimer: Difference between revisions
From Onset Developer Wiki
No edit summary |
No edit summary |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 8: | Line 8: | ||
{{FuncParam|LuaFunction|The function to execute.}} | {{FuncParam|LuaFunction|The function to execute.}} | ||
{{FuncParam|Interval|Interval to execute the function.}} | {{FuncParam|Interval|Interval to execute the function.}} | ||
{{FuncParam|Count| | {{FuncParam|Count|How often to execute this timer. Use [[CreateTimer]] for a non-stop version.}} | ||
{{FuncParamOptional|UserArgs|Arguments to pass to the function.}} | {{FuncParamOptional|UserArgs|Arguments to pass to the function.}} | ||
{{FuncReturnValue|Returns the identifier to this timer. Use DestroyTimer to stop it.}} | {{FuncReturnValue|Returns the identifier to this timer. Use [[DestroyTimer]] to stop it.}} | ||
== Example == | == Example == | ||
<syntaxhighlight lang="Lua"> | |||
function cmd_vcolorfun(player) | |||
local vehicle = GetPlayerVehicle(player) | |||
if (vehicle == 0) then | |||
return AddPlayerChat(player, "You must be in a vehicle") | |||
end | |||
if (GetPlayerVehicleSeat(player) ~= 1) then | |||
return AddPlayerChat(player, "You must be the driver of the vehicle") | |||
end | |||
--Change color every 200ms of the players vehicle | |||
CreateCountTimer(function(vehicle) | |||
if not IsValidVehicle(vehicle) then | |||
return | |||
end | |||
local r = Random(100, 255) | |||
local g = Random(100, 255) | |||
local b = Random(100, 255) | |||
SetVehicleColor(vehicle, RGB(r, g, b)) | |||
end, 200, 16, vehicle) | |||
end | |||
AddCommand("vcolorfun", cmd_vcolorfun) | |||
</syntaxhighlight> | |||
{{RelatedFunctions}} | {{RelatedFunctions}} | ||
{{Template:TimerFunctions}} |
Latest revision as of 11:21, 1 September 2020
Description
Creates a countdown timer.
Syntax
CreateCountTimer(LuaFunction, Interval, Count [, UserArgs])
Parameters
- LuaFunction
The function to execute. - Interval
Interval to execute the function. - Count
How often to execute this timer. Use CreateTimer for a non-stop version. - UserArgs (optional)
Arguments to pass to the function.
Return Value
- Returns the identifier to this timer. Use DestroyTimer to stop it.
Example
function cmd_vcolorfun(player)
local vehicle = GetPlayerVehicle(player)
if (vehicle == 0) then
return AddPlayerChat(player, "You must be in a vehicle")
end
if (GetPlayerVehicleSeat(player) ~= 1) then
return AddPlayerChat(player, "You must be the driver of the vehicle")
end
--Change color every 200ms of the players vehicle
CreateCountTimer(function(vehicle)
if not IsValidVehicle(vehicle) then
return
end
local r = Random(100, 255)
local g = Random(100, 255)
local b = Random(100, 255)
SetVehicleColor(vehicle, RGB(r, g, b))
end, 200, 16, vehicle)
end
AddCommand("vcolorfun", cmd_vcolorfun)