That line doesn't start a countdown the way we humans would do a countdown. It's more like looking at the current time (which is, say, 9:30), deciding you want to go out after 5 minutes, and then writing down '9:35'. You only do that at the start - if you did that again at, say, 9:33, then you'd overwrite the original 9:35 with 9:33 + 5 = 9:38, which essentially just postpones things.
The real work happens in
Think
- that's similar to looking at the clock every minute or so to see whether it's 9:35 already. Is 9:30 at or past 9:35? No. Is 9:31 at or past 9:35? No. Is 9:32 at or past 9:35? No. ... Until finally at 9:35, the answer is yes, and you go out.
Computers are good at following orders, but they're not smart, so you have to be very precise with your instructions. 9:36 is also past 9:35, so a computer will happily decide to go out again. The same happens at 9:37, 9:38, and so on. Overwriting the original 9:35 with 0:00 won't help, because 9:36 is also past 0:00, so that's why you need an extra check: do I need to go outside? We people just 'know' what we want, but for a computer you really need to spell out all those little details.
You can make the code easier to understand by creating a few small functions:
void startAutoReloadTimer(float seconds)
{
if (!isAutoReloadTimerEnabled()) // Only schedule a new auto-reload if we're not waiting for one already.
m_flAutoReloadTime = gpGlobals->time + seconds;
}
bool isAutoReloadTimerEnabled() { return m_flAutoReloadTime > 0; }
void stopAutoReloadTimer() { m_flAutoReloadTime = 0; }
and use those throughout your code:
if (isAutoReloadTimerEnabled() && gpGlobals->time >= m_flAutoReloadTime)
{
autoReload();
stopAutoReloadTimer();
}
A simpler variant is to just set
m_flAutoReloadTime
to a really big value, like 'next year'. I prefer the above approach however, because it's more obvious that the timer can be stopped.