So do timers and alarms have the same function or should I be using them in different situations? Like in this example, would it have been easier to simply use the timer since I was trying to delay the step event? Aside from that, thank you for the clarification - definitely something I must keep in mind from now on.
So, I'm going to use a smidgen more complexity here, but it might help you understand the timers a little bit better. The first thing to keep in mind is NOTHING HAPPENS SIMULTANEOUSLY. NOTHING. Every instruction, and every event has to happen one at a time. The timers and alarms are just there to simplify things, but they exist outside of the step event and are called when they are called. If you made your
own timer, it would look like this:
Code:
// create event
myTimer = 60;
Code:
// step event
if myTimer > 0 {
myTimer = myTimer - 1;
if myTimer == 0 {
// alarm
myTimer = -1;
}
}
Timers are incredibly simple. All the built-in timers are doing is allowing you to avoid writing that block of code a bunch of times if you have a lot of timers you want to use. To digress ever so slightly, this is how timing in
general works, simply asking the program to not do stuff until a criteria has been met. In the case of the timer, that criteria is that it is no longer running, which we know because it is equal to -1. So, to get back to your question specifically, it all depends. Let's extrapolate this a bit and ask: how would you make the game pause? You might say, 'okay, I have this timer, and as long as it isn't -1 the character can't move' so you might just choose to set the timer. Well, that would lead to a situation where you could only pause for a set amount of time. To fix this you might either choose a really, really high number expecting no one will pause the game for a thousand minutes, or you could just keep setting the timer. However, now we have a
new issue, what about all the
other things that need to be paused? Like enemies and animations? Do we have to set timers for all of
them too? The answer is no, we could just have all of them share the
same timer. Or, since it's pointless for pausing, we could just drop the timer altogether and make ourselves a global variable:
Code:
if global.pause == true { exit }
The reason I bring up this exact example is because where, and why, you use not just a timer, but a
condition is definitely important. This quickly becomes a discussion about scope, but to simplify: a program is literally a series of smaller and smaller ideas. A 'game' is made up of 'levels', and those 'levels' are made up of 'characters' and 'obstacles' and 'characters' are made up of the 'player' and 'enemies'. So when we decide to implement something like a pause, the question is, at what level (or scope) is this appropriate? Should the
entire game be paused when we press start? Or perhaps just the level? Or even maybe just the characters? This will determine both where we define that 'pause', and what objects need it as a condition.
That's a bit of a large, wide answer so I'll leave it at that for now. If you have more questions, feel free to ask them.