CreateTimer: Difference between revisions
From Onset Developer Wiki
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
{{Info|Function|Server & Client|1.0}} | {{Info|Function|Server & Client|1.0}} | ||
{{FuncDescription| | {{FuncDescription|Creates a timer to call a latent function.}} | ||
{{FuncSyntax|CreateTimer(LuaFunction, Interval [, UserArgs])}} | {{FuncSyntax|CreateTimer(LuaFunction, Interval [, UserArgs])}} | ||
{{FuncParameters}} | {{FuncParameters}} | ||
{{FuncParam|LuaFunction| | {{FuncParam|LuaFunction|The function to execute.}} | ||
{{FuncParam|Interval | | {{FuncParam|Interval |Interval to execute the function.}} | ||
{{FuncParamOptional|UserArgs| | {{FuncParamOptional|UserArgs|Arguments to pass to the function.}} | ||
{{FuncReturnValue|__EDIT_ME__}} | {{FuncReturnValue|__EDIT_ME__}} | ||
== 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 | |||
CreateTimer(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, vehicle) | |||
end | |||
AddCommand("vcolorfun", cmd_vcolorfun) | |||
</syntaxhighlight> | |||
{{RelatedFunctions}} | {{RelatedFunctions}} | ||
__EDIT_ME__ | __EDIT_ME__ |
Revision as of 16:44, 26 September 2019
Description
Creates a timer to call a latent function.
Syntax
CreateTimer(LuaFunction, Interval [, UserArgs])
Parameters
- LuaFunction
The function to execute. - Interval
Interval to execute the function. - UserArgs (optional)
Arguments to pass to the function.
Return Value
- __EDIT_ME__
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
CreateTimer(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, vehicle)
end
AddCommand("vcolorfun", cmd_vcolorfun)
See also
__EDIT_ME__