PHP Code:
Handle g_hTimer; // Store Timer Handle
float g_fTimerTime; // Value to parse to timer, build through ConVars or harcode inside your functions.
float g_fTimerStore = -1.0; // Store here timer durations for pause/resume (IMPORTANT, NEG VALUES MEAN TIMER NOT CREATED)
void SomethingThatEnablesTimer()
{
/*
* Whatever you want to do before starting Timer here
* You don't need to add a new function only for this, just copy the 3 lines below in the function that enables this timer
* Unless timer is being called from multiple functions, then don't make redundant code
*/
delete g_hTimer; // Safe way to get rid of old timer if exists and prevent double timer execution
g_hTimer = CreateTimer(g_fTimerTime, Pausable_Timer); // Create the timer
g_fTimerStore = GetGameTime() + g_fTimerTime; // Store when the timer will end
}
/*
* Is not possible to pause a timer, you only can stop and store remaining time and create
* a new one and then assing the reaining time.
* You can call PauseTheTimer() and ContinueTimer() multiple times if you want
*/
void PauseTheTimer()
{
// Trying to pause the timer when it doesn't exist? Nope!
if( g_fTimerStore < 0.0 )
return;
g_fTimerStore -= GetGameTime(); // Substract time to get the remaining time of the timer when stopped
delete g_hTimer; // No method to pause, delete it
}
void ResumeTheTimer()
{
// Ehhh.... NO!
if( g_fTimerStore < 0.0 )
return;
delete g_hTimer; // Not needed if you have write the code well, but prevents possible errors
g_hTimer = CreateTimer(g_fTimerStore, Pausable_Timer);
g_fTimerStore += GetGameTime(); // Again convert timer store to the time when the timer will end
}
Action Pausable_Timer(Handle timer, any data)
{
g_hTimer = null; // Allways when you store timer Handles
g_fTimerStore = -1.0; // To prevent to pause/continue timers when there is no timer
/*
* Your Timer code here
*/
return Plugin_Continue; // Allways on Actions
}
__________________