AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Plugin/Gameplay Ideas and Requests (https://forums.alliedmods.net/forumdisplay.php?f=60)
-   -   Solved Is there a native to pause timer (https://forums.alliedmods.net/showthread.php?t=338885)

alasfourom 08-01-2022 13:19

Is there a native to pause timer
 
Hello Everyone,

I want an easy way to pause and resume timer, lets take this script by "RU_6uK" as an example

PHP Code:

float g_fSeconds;
bool g_bRoundEnd;

public 
void OnPluginStart()
{
    
HookEvent("player_left_safe_area"Event_LeftStartArea);
    
HookEvent("round_end"Event_RoundEnd);

    
RegConsoleCmd("sm_duration"Command_Duration"Display Round Duration To Chat");
}

public 
Action Event_LeftStartArea(Event event, const char[] namebool dontBroadcast)
{
    
g_fSeconds GetEngineTime();
    
g_bRoundEnd false;
    return 
Plugin_Handled;
}

public 
Action Event_RoundEnd(Event event, const char[] namebool dontBroadcast)
{
    if(!
g_bRoundEndg_bRoundEnd true;    
    return 
Plugin_Handled;
}

public 
Action Command_Duration(int clientint args)
{
    
int secDiff RoundToFloor(GetEngineTime() - g_fSeconds);
    
ReplyToCommand(client"\x04Round Duration: \x03%i min %i sec"secDiff 60secDiff 60);
    return 
Plugin_Handled;


Any method to pause it, is acceptable, with a command or event

Thanks

alasfourom 08-01-2022 13:24

Re: What IS The Simplest Way To Pause Timer
 
Wrong section :roll:

Marttt 08-01-2022 14:13

Re: What IS The Simplest Way To Pause Timer
 
delete and create the timer
or
use return Plugin_Continue; while some condition is false.

alasfourom 08-01-2022 15:17

Re: What IS The Simplest Way To Pause Timer
 
That will reset the timer, I want it in a way like it passed 30 seconds, I can pause the 30 sec until a condition is triggered then resume the 30 seconds again.

for example the round duration is now 5 min, I want to pause it, then allow it to continue 5:01 and so on, not resting it to 0 and then starting all over again

boink 08-01-2022 18:57

Re: What IS The Simplest Way To Pause Timer
 
There is no native way to 'pause' a timer, you will need to assign the timer handle to a variable, and keep track of the round time yourself. When you need to pause it, you delete/kill the timer, then when you start it again, create the timer again and subtract whatever time has passed since you first started it + the amount of time since pausing it.

Alternatively, you could have a repeating timer at a 1.0 second interval and manually subtract the time from within the timer callback, when it's in a 'paused' state, don't subtract the time and exit the function. Once the time variable you have set hits 0, run whatever code you need.

alasfourom 08-01-2022 23:38

Re: What IS The Simplest Way To Pause Timer
 
Got it, thank you

Earendil 08-02-2022 13:24

Re: What IS The Simplest Way To Pause Timer
 
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_fTimerTimePausable_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_fTimerStorePausable_Timer);
    
g_fTimerStore += GetGameTime(); // Again convert timer store to the time when the timer will end
}

Action Pausable_Timer(Handle timerany 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



alasfourom 08-02-2022 14:24

Re: What IS The Simplest Way To Pause Timer
 
Thats more than clear now Earendil, Thank you very much as always :fox:


All times are GMT -4. The time now is 05:48.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.