Difference between revisions of "HPL3/Scripting/Scripting Guide/Timers"
Line 4: | Line 4: | ||
Timers are set up to wait for a selected amount of time before executing code. It can be very useful if you want to wait a specified amount of time for something to happen in the level, such as intense countdown or to spawn / despawn particles in a dynamic way. | Timers are set up to wait for a selected amount of time before executing code. It can be very useful if you want to wait a specified amount of time for something to happen in the level, such as intense countdown or to spawn / despawn particles in a dynamic way. | ||
− | == Creating Timers == | + | ==Creating Timers== |
− | In order to add timer to a map, we need to use <code>Map_AddTimer</code>. Let's see an example: | + | In order to add timer to a map, we need to use the function <code>Map_AddTimer</code>. Let's see an example:<syntaxhighlight lang="c++"> |
+ | //////////////////////////// | ||
+ | // Run first time starting map | ||
+ | void OnStart() | ||
+ | { | ||
+ | Map_AddTimer("JumpVeryHigh", 5.0f, "Timer_JumpVeryHigh"); | ||
+ | } | ||
− | + | //...somewhere else in the code | |
+ | |||
+ | void Timer_JumpVeryHigh(const tString&in asTimer) | ||
+ | { | ||
+ | MyHelper_MakeThePlayerJumpVeryHigh(); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ===Breakdown=== | ||
+ | |||
+ | * We called the function and gave it three arguments: The internal name of the timer, the time (in seconds) of the timer, and the function callback which will be called once the timer is done. | ||
+ | * Our timer callback is called "Timer_JumpVeryHigh" and has one parameter called asTimer - this is the internal name of the timer. In our case, the internal name will be "JumpVeryHigh". | ||
We can do more than just create a timer, we can remove timers as well, or check if a timer is already running. For example, if we enter this area, create a timer of 5 seconds. However, if a timer has already been created, we don’t need to create that timer again, right? So we can use Map_TimerExists for that. | We can do more than just create a timer, we can remove timers as well, or check if a timer is already running. For example, if we enter this area, create a timer of 5 seconds. However, if a timer has already been created, we don’t need to create that timer again, right? So we can use Map_TimerExists for that. | ||
Line 13: | Line 30: | ||
Timers are very easy to use as you can see, and it will help us to understand the concept of Sequences in the next episode. | Timers are very easy to use as you can see, and it will help us to understand the concept of Sequences in the next episode. | ||
− | == See Also == | + | ==See Also== |
− | * [[HPL3/Scripting/Map Helper|Map Helper]] | + | |
+ | *[[HPL3/Scripting/Map Helper|Map Helper]] | ||
{{NavBar|HPL3/Scripting/Scripting_Guide/The Update method|The Update method|HPL3/Scripting/HPL3 Scripting Guide|HPL3 Scripting Guide|HPL3/Scripting/Scripting_Guide/Sequences|Sequences}} | {{NavBar|HPL3/Scripting/Scripting_Guide/The Update method|The Update method|HPL3/Scripting/HPL3 Scripting Guide|HPL3 Scripting Guide|HPL3/Scripting/Scripting_Guide/Sequences|Sequences}} |
Revision as of 22:18, 13 August 2020
Timers are set up to wait for a selected amount of time before executing code. It can be very useful if you want to wait a specified amount of time for something to happen in the level, such as intense countdown or to spawn / despawn particles in a dynamic way.
Creating Timers
In order to add timer to a map, we need to use the function Map_AddTimer
. Let's see an example:
////////////////////////////
// Run first time starting map
void OnStart()
{
Map_AddTimer("JumpVeryHigh", 5.0f, "Timer_JumpVeryHigh");
}
//...somewhere else in the code
void Timer_JumpVeryHigh(const tString&in asTimer)
{
MyHelper_MakeThePlayerJumpVeryHigh();
}
Breakdown
- We called the function and gave it three arguments: The internal name of the timer, the time (in seconds) of the timer, and the function callback which will be called once the timer is done.
- Our timer callback is called "Timer_JumpVeryHigh" and has one parameter called asTimer - this is the internal name of the timer. In our case, the internal name will be "JumpVeryHigh".
We can do more than just create a timer, we can remove timers as well, or check if a timer is already running. For example, if we enter this area, create a timer of 5 seconds. However, if a timer has already been created, we don’t need to create that timer again, right? So we can use Map_TimerExists for that.
Timers are very easy to use as you can see, and it will help us to understand the concept of Sequences in the next episode.