timer.Create
From GMod Wiki
Function | |
Syntax |
timer.Create( Var uniqueID, Float delay, Integer reps, Function func, Var ... arguments ) Where is this used? |
Description: | |
Creates a timer that runs a function with the given arguments when it expires. "reps" can be either the number of times the timer will run, or 0 if the timer will run forever. |
|
Returns: | nil |
Part of Library: | Timer |
Realm: | |
BBCode Link: | [b][url=http://wiki.garrysmod.com/?title=Timer.Create]Timer.Create [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] |
Examples
Description | Displays "Test" in the console once after 10 seconds. |
---|---|
Used on | |
Code | timer.Create("timer1", 10, 1, MsgN, "Test") |
Output | (After 10 seconds has elapsed) "Test" |
Description | Displays "Hi!" in the console every 10 seconds forever. |
---|---|
Used on | |
Code | timer.Create( "my_timer", 10, 0, function() MsgN("Hi!") end) |
Output | (Every 10 seconds) "Hi!" |
Additional Notes
- uniqueID can be a String or an Integer . The uniqueID ensures that only one timer with that "unique ID" is running at a time (so old timers with that ID get cancelled before the new one starts). The unique ID can also be used to stop a running timer before it ends.
- delay is how long (in seconds) to wait before running the function. If the delay is less-than or equal to 0, the timer will run every frame.
- reps is how many times to run the timer before it ends. Each time the timer runs it will call the given function with the given arguments. Usually this should be 1. If this is 0, the timer will keep running until stopped with timer.Stop / timer.Remove / timer.Destroy.
- func should be the name of a function to run when the timer expires. The function will be run with the given arguments.
- argument can be anything you like. It will be an argument in func. For example, if you wrote timer.Create("Unique",1,1,Msg,"Hello\n"), then one second later it would do Msg("Hello\n").
- ... means "limitless arguments like the last one". In other words, you can add more than one argument to func the same way you added the first one.
- As mentioned above, if a timer with the same Unique ID has already been created, the old one gets canceled and the new one starts.
- If you want to make a new timer without starting it, use timer.Adjust (It takes all the same parameters). Then call timer.Start("timer_name") when you're ready to let it go.
- Lua invokes member functions by passing the object as the first parameter to the function.
If you were to call self:SetHealth(100), which can be written as self.SetHealth(self,100), you would create a timer with timer.Create("timer1", 1, 0, self.SetHealth, self, 100).
This would set self's health to 100 every second.