SetTimer: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 7: Line 7:
The minimum accepted interval is 50ms.
The minimum accepted interval is 50ms.


==Syntax==  
==Syntax==
<section name="Server and Client" class="both" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
timerID setTimer ( function theFunction, int timeInterval, int timesToExecute, [var argument1, ...] )
timer setTimer ( function theFunction, int timeInterval, int timesToExecute, [ var arguments... ] )
</syntaxhighlight>  
</syntaxhighlight>  


Line 15: Line 16:
*'''theFunction:''' The function you wish the timer to call.
*'''theFunction:''' The function you wish the timer to call.
*'''timeInterval:''' The number of milliseconds that should elapse before the function is called. 1000 milliseconds = 1 second.
*'''timeInterval:''' The number of milliseconds that should elapse before the function is called. 1000 milliseconds = 1 second.
*'''timesToExecute:''' The number of times you want the timer to execute. 0 = infinite.
*'''timesToExecute:''' The number of times you want the timer to execute, or 0 for infinite repetitions.


===Optional Arguments===  
===Optional Arguments===  
{{OptionalArg}}  
{{OptionalArg}}  
*'''argument1:''' Any arguments you wish to pass to the function can be listed after the ''repeats'' argument.
*'''arguments:''' Any arguments you wish to pass to the function can be listed after the ''repeats'' argument.


===Returns===
===Returns===
Returns ''timerID'' if the timer was set succesfully, ''false'' if the arguments are invalid or the timer could not be set.
Returns a [[timer]] pointer if the timer was set succesfully, ''false'' if the arguments are invalid or the timer could not be set.
</section>


==Example==  
==Example==
<section name="Example" class="both" show="true">
This example will output some text after a small delay.
This example will output some text after a small delay.


<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Define function to be called
-- define function to be called
function delayedChat ( text )
function delayedChat ( text )
outputChatBox ( "Delayed text: " .. text )
outputChatBox ( "Delayed text: " .. text )
end
end


-- Set a timer so the function is called after 1 second
-- set a timer so the function is called after 1 second
setTimer ( delayedChat, 1000, 1, "Hello, World!" )
setTimer ( delayedChat, 1000, 1, "Hello, World!" )
</syntaxhighlight>
</syntaxhighlight>
1 second after the line above has been executed, the text ''Delayed text: Hello, World!'' will be displayed in the chat box.
1 second after the line above has been executed, the text ''Delayed text: Hello, World!'' will be displayed in the chat box.
</section>


==See Also==
==See Also==
{{Utility functions}}
{{Utility functions}}

Revision as of 19:48, 26 August 2007

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.

Syntax

Click to collapse [-]
Server and Client
timer setTimer ( function theFunction, int timeInterval, int timesToExecute, [ var arguments... ] )

Required Arguments

  • theFunction: The function you wish the timer to call.
  • timeInterval: The number of milliseconds that should elapse before the function is called. 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 repeats argument.

Returns

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

Example

Click to collapse [-]
Example

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.

See Also