SetTimer: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
Temporary Timer Explanation
__NOTOC__
This function allows you to trigger a function after a number of milliseconds have ellapsed. You can call either your own functions, or built in functions. For example, you could set a timer to spawn a player after a number of seconds had ellapsed.


[17:33] <Ransom> yes.. explain how ur damn settimers can work
Once a timer has called a function once, it no longer exists. To repeatedly call a function, have that function recreate the timer each time.
[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... )
==Syntax==
[17:34] <ChrML_Laptop> first argument is the name of the function to run in the given number of milliseconds
<syntaxhighlight lang="lua">
[17:34] <slush> heh
bool setTimer ( string functionName, int time, [var argument1, ...] )
[17:34] <Ransom> okay.. so it makes a function or it can be used as... a command
</syntaxhighlight>  
[17:34] <slush> i wonder how it would be to play gta with my xbox 360 control
 
[17:34] <slush> since it does work with windows and all
===Required Arguments===
[17:34] <Talidan> it doesnt make any functions
*'''functionName:''' The name of the function you wish the timer to call.
[17:35] <Talidan> it launches a function
*'''time:''' The number of milliseconds that should ellapse before the function is called. 1000 milliseconds = 1 second.
[17:35] <ChrML_Laptop> what's in the quote is simply the name of the function
 
[17:35] <ChrML_Laptop> then there's a time argument
===Optional Arguments===
[17:35] <ChrML_Laptop> then there are N arguments that you'd normally put in the ()
{{OptionalArg}}
[17:35] <ChrML_Laptop> setTimer ( "serverChat", 5000, "hi girls" )
*'''argument1:''' Any arguments you wish to pass to the function can be listed after the ''time'' variable.
[17:35] <Talidan> chrml he understands that, its something else he isnt getting and i dont get what he means
 
[17:36] <Ransom>  onExitVehicle ( player, vehicle, int seat, jackingplayer )
===Returns===
[17:36] <Ransom>        destroyvehicle = 1
Returns ''true'' if the timer was set succesfully, ''false'' if the arguments are invalid or the timer could not be set.
[17:36] <Ransom>      SetTimer ( check, 10000 )
 
[17:36] <Ransom>  end
==Example==
[17:36] <Ransom>  then
'''Example 1:'' This example will output some text after a number of seconds.
[17:36] <Ransom>  function check ()
 
[17:36] <Ransom>      if destroyvehicle = 1
First we define a function that we wish to be called.
[17:36] <Ransom>          SetTimer ( setVehicleHealth, 10000, vehicle, 0 )
<syntaxhighlight lang="lua">
[17:36] <Ransom>  end
function delayedChat ( text )
[17:36] <Talidan> 'then' isnt part of the code
outputChatBox ( "Delayed text: " .. text )
[17:36] <Ransom> he just created a function through a timer... then said onthatfunction, then  used another timer inside it as a countdown
end
[17:36] <Talidan> and its all ==
</syntaxhighlight>
[17:36] <ChrML_Laptop> that code looks syntax error
 
[17:36] <ChrML_Laptop> function onExitVehicle ( blablabl )
Then we can set a timer that will call it in 1 second (1000 milliseconds) time.
[17:37] <Talidan> i c/p chrml
<syntaxhighlight lang="lua">
[17:37] <Talidan> trying to work out the logic though chrml
setTimer ( "delayedChat", 1000, "Hello, World!" )
[17:37] <ChrML_Laptop> always == for comparison
</syntaxhighlight>
[17:37] <Talidan> how would you destroy a vehicle after inactivity
1 second after the line above has been executed, the text ''Delayed text: Hello, World!'' will be displayed in the chat box.
[17:37] <ChrML_Laptop> if ( test = 5 ) then
 
[17:37] <Ransom> so you can set timers to check made up function names right...
==See Also==
[17:37] <ChrML_Laptop> will assign 5 to test, then check if it's >0
{{Utility_Functions}}
[17:37] <Ransom> thats what you are saying
[17:37] <ChrML_Laptop> atleast it will in C+
[17:37] <ChrML_Laptop> ++
[17:37] <Talidan> yeah ransom
[17:37] <Talidan> functions you make up dont go in ""
[17:37] <Talidan> functions that are on the wiki do go in ""
[17:37] <Ransom> okay but yet they can also be used to perform functions at given countdowns
[17:37] <Ransom> ?
[17:37] <Talidan> wait no
[17:37] <Talidan> iim wrong
[17:38] <Talidan> they all go in ""
[17:38] <ChrML_Laptop> yes
[17:38] <Ransom> and all those arg1, arg2 just depend on if you put a bigass function in there with lots arguments?
[17:38] <Talidan> yeah
[17:38] <Talidan> well ask chrml to write up the code anyway ransom
[17:38] <Talidan> he'd have better logic
[17:39] <Ransom> well ill just paste this in the wiki for temporary reference :P

Revision as of 13:02, 20 May 2006

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

Once a timer has called a function once, it no longer exists. To repeatedly call a function, have that function recreate the timer each time.

Syntax

bool setTimer ( string functionName, int time, [var argument1, ...] )

Required Arguments

  • functionName: The name of the function you wish the timer to call.
  • time: The number of milliseconds that should ellapse before the function is called. 1000 milliseconds = 1 second.

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.

  • argument1: Any arguments you wish to pass to the function can be listed after the time variable.

Returns

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

Example

'Example 1: This example will output some text after a number of seconds.

First we define a function that we wish to be called.

function delayedChat ( text )
	outputChatBox ( "Delayed text: " .. text )
end

Then we can set a timer that will call it in 1 second (1000 milliseconds) time.

setTimer ( "delayedChat", 1000, "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

Template:Utility Functions