SetTimer: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
 
(Added a note about how clients can mess with timer speed)
(41 intermediate revisions by 21 users not shown)
Line 1: Line 1:
Temporary Timer Explanation
__NOTOC__
{{Server client function}}
This function allows you to trigger a function after a number of milliseconds have elapsed. You can call one of your own functions or a built-in function. For example, you could set a timer to spawn a player after a number of seconds have elapsed.


[17:33] <Ransom> yes.. explain how ur damn settimers can work
Once a timer has finished repeating, it no longer exists.
[17:33] <Talidan> ransom wants to destroy a vehicle after a certain time of inactivity
 
[17:33] <ChrML_Laptop> setTimer ( "function", time in ms, arg1, arg2, argn... )
'''The minimum accepted interval is 50ms.'''
[17:34] <ChrML_Laptop> first argument is the name of the function to run in the given number of milliseconds
 
[17:34] <slush> heh
'''Note: The speed at which a client side timer runs can be completely unreliable if a client is maliciously modifying their operating system speed, timers could run much faster or slower.'''
[17:34] <Ransom> okay.. so it makes a function or it can be used as... a command
 
[17:34] <slush> i wonder how it would be to play gta with my xbox 360 control
Multi Theft Auto guarantees that the timer will be triggered after ''at least'' the interval you specify. The resolution of the timer is tied to the frame rate (server side and client-side). All the overdue timers are triggered at a single point each frame. This means that if, for example, the player is running at 30 frames per second, then two timers specified to occur after 100ms and 110ms would more than likely occur during the same frame, as the difference in time between the two timers (10ms) is less than half the length of the frame (33ms). As with most timers provided by other languages, you shouldn't rely on the timer triggering at an exact point in the future.
[17:34] <slush> since it does work with windows and all
 
[17:34] <Talidan> it doesnt make any functions
==Syntax==
[17:35] <Talidan> it launches a function
<syntaxhighlight lang="lua">
[17:35] <ChrML_Laptop> what's in the quote is simply the name of the function
timer setTimer ( function theFunction, int timeInterval, int timesToExecute, [ var arguments... ] )
[17:35] <ChrML_Laptop> then there's a time argument
</syntaxhighlight>  
[17:35] <ChrML_Laptop> then there are N arguments that you'd normally put in the ()
{{OOP||[[Timer]]}}
[17:35] <ChrML_Laptop> setTimer ( "serverChat", 5000, "hi girls" )
===Required Arguments===
[17:35] <Talidan> chrml he understands that, its something else he isnt getting and i dont get what he means
*'''theFunction:''' The function you wish the timer to call.
[17:36] <Ransom>  onExitVehicle ( player, vehicle, int seat, jackingplayer )
{{Note|The hidden global variable '''sourceTimer''' contains the currently executing timer userdata}}
[17:36] <Ransom>        destroyvehicle = 1
*'''timeInterval:''' The number of milliseconds that should elapse before the function is called. (the minimum is 50; 1000 milliseconds = 1 second)
[17:36] <Ransom>       SetTimer ( check, 10000 )
*'''timesToExecute:''' The number of times you want the timer to execute, or 0 for infinite repetitions.
[17:36] <Ransom>  end
 
[17:36] <Ransom>  then
===Optional Arguments===
[17:36] <Ransom>  function check ()
{{OptionalArg}}
[17:36] <Ransom>      if destroyvehicle = 1
*'''arguments:''' Any arguments you wish to pass to the function can be listed after the ''timesToExecute'' argument. Note that any tables you want to pass will get cloned, whereas metatables and functions/function references in that passed table will get lost. Also changes you make in the original table before the function gets called won't get transferred.
[17:36] <Ransom>          SetTimer ( setVehicleHealth, 10000, vehicle, 0 )
 
[17:36] <Ransom> end
===Returns===
[17:36] <Talidan> 'then' isnt part of the code
Returns a [[timer]] pointer if the timer was set successfully, ''false'' if the arguments are invalid or the timer could not be set.
[17:36] <Ransom> he just created a function through a timer... then said onthatfunction, then used another timer inside it as a countdown
 
[17:36] <Talidan> and its all ==
==Examples==
[17:36] <ChrML_Laptop> that code looks syntax error
This example will output some text after a small delay.
[17:36] <ChrML_Laptop> function onExitVehicle ( blablabl )
 
[17:37] <Talidan> i c/p chrml
<syntaxhighlight lang="lua">
[17:37] <Talidan> trying to work out the logic though chrml
-- define function to be called
[17:37] <ChrML_Laptop> always == for comparison
function delayedChat ( text )
[17:37] <Talidan> how would you destroy a vehicle after inactivity
outputChatBox ( "Delayed text: " .. text )
[17:37] <ChrML_Laptop> if ( test = 5 ) then
end
[17:37] <Ransom> so you can set timers to check made up function names right...
 
[17:37] <ChrML_Laptop> will assign 5 to test, then check if it's >0
-- set a timer so the function is called after 1 second
[17:37] <Ransom> thats what you are saying
setTimer ( delayedChat, 1000, 1, "Hello, World!" )
[17:37] <ChrML_Laptop> atleast it will in C+
</syntaxhighlight>
[17:37] <ChrML_Laptop> ++
 
[17:37] <Talidan> yeah ransom
1 second after the line above has been executed, the text ''Delayed text: Hello, World!'' will be displayed in the chat box.
[17:37] <Talidan> functions you make up dont go in ""
 
[17:37] <Talidan> functions that are on the wiki do go in ""
This example will nest a whole function within a timer. This is nice for things like setting variables without having to call a function outside of your code block.
[17:37] <Ransom> okay but yet they can also be used to perform functions at given countdowns
 
[17:37] <Ransom> ?
<syntaxhighlight lang="lua">
[17:37] <Talidan> wait no
function mainFunction()
[17:37] <Talidan> iim wrong
        outputChatBox ("Instant text!")
[17:38] <Talidan> they all go in ""
setTimer ( function()
[17:38] <ChrML_Laptop> yes
outputChatBox ( "5 second delay text!" )
[17:38] <Ransom> and all those arg1, arg2 just depend on if you put a bigass function in there with lots arguments?
end, 5000, 1 )
[17:38] <Talidan> yeah
end
[17:38] <Talidan> well ask chrml to write up the code anyway ransom
 
[17:38] <Talidan> he'd have better logic
mainFunction() --call function
[17:39] <Ransom> well ill just paste this in the wiki for temporary reference :P
</syntaxhighlight>
 
This example outputs some random text to the chat every 300000 milliseconds (5 minutes).
<syntaxhighlight lang="lua">
setTimer(function()
    outputChatBox("Text " .. math.random(1,4))
end, 300000, 0)
</syntaxhighlight>
 
==See Also==
{{Utility functions}}

Revision as of 21:21, 5 July 2018

This function allows you to trigger a function after a number of milliseconds have elapsed. You can call one of your own functions or a built-in function. For example, you could set a timer to spawn a player after a number of seconds have elapsed.

Once a timer has finished repeating, it no longer exists.

The minimum accepted interval is 50ms.

Note: The speed at which a client side timer runs can be completely unreliable if a client is maliciously modifying their operating system speed, timers could run much faster or slower.

Multi Theft Auto guarantees that the timer will be triggered after at least the interval you specify. The resolution of the timer is tied to the frame rate (server side and client-side). All the overdue timers are triggered at a single point each frame. This means that if, for example, the player is running at 30 frames per second, then two timers specified to occur after 100ms and 110ms would more than likely occur during the same frame, as the difference in time between the two timers (10ms) is less than half the length of the frame (33ms). As with most timers provided by other languages, you shouldn't rely on the timer triggering at an exact point in the future.

Syntax

timer setTimer ( function theFunction, int timeInterval, int timesToExecute, [ var arguments... ] )

OOP Syntax Help! I don't understand this!

Method: Timer(...)


Required Arguments

  • theFunction: The function you wish the timer to call.
[[{{{image}}}|link=|]] Note: The hidden global variable sourceTimer contains the currently executing timer userdata
  • timeInterval: The number of milliseconds that should elapse before the function is called. (the minimum is 50; 1000 milliseconds = 1 second)
  • timesToExecute: The number of times you want the timer to execute, or 0 for infinite repetitions.

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • arguments: Any arguments you wish to pass to the function can be listed after the timesToExecute argument. Note that any tables you want to pass will get cloned, whereas metatables and functions/function references in that passed table will get lost. Also changes you make in the original table before the function gets called won't get transferred.

Returns

Returns a timer pointer if the timer was set successfully, false if the arguments are invalid or the timer could not be set.

Examples

This example will output some text after a small delay.

-- define function to be called
function delayedChat ( text )
	outputChatBox ( "Delayed text: " .. text )
end

-- set a timer so the function is called after 1 second
setTimer ( delayedChat, 1000, 1, "Hello, World!" )

1 second after the line above has been executed, the text Delayed text: Hello, World! will be displayed in the chat box.

This example will nest a whole function within a timer. This is nice for things like setting variables without having to call a function outside of your code block.

function mainFunction()
        outputChatBox ("Instant text!")
	setTimer ( function()
		outputChatBox ( "5 second delay text!" )
	end, 5000, 1 )
end

mainFunction() --call function

This example outputs some random text to the chat every 300000 milliseconds (5 minutes).

setTimer(function()
    outputChatBox("Text " .. math.random(1,4))
end, 300000, 0)

See Also