Raised This Month: $12 Target: $400
 3% 

Solved Is there a native to pause timer


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
alasfourom
Senior Member
Join Date: Feb 2022
Location: Saudi Arabia
Old 08-01-2022 , 13:19   Is there a native to pause timer
Reply With Quote #1

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

Last edited by alasfourom; 12-26-2022 at 04:18.
alasfourom is offline
alasfourom
Senior Member
Join Date: Feb 2022
Location: Saudi Arabia
Old 08-01-2022 , 13:24   Re: What IS The Simplest Way To Pause Timer
Reply With Quote #2

Wrong section
alasfourom is offline
Marttt
Veteran Member
Join Date: Jan 2019
Location: Brazil
Old 08-01-2022 , 14:13   Re: What IS The Simplest Way To Pause Timer
Reply With Quote #3

delete and create the timer
or
use return Plugin_Continue; while some condition is false.
__________________
Marttt is offline
alasfourom
Senior Member
Join Date: Feb 2022
Location: Saudi Arabia
Old 08-01-2022 , 15:17   Re: What IS The Simplest Way To Pause Timer
Reply With Quote #4

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

Last edited by alasfourom; 08-01-2022 at 15:19.
alasfourom is offline
boink
Member
Join Date: May 2021
Location: Australia
Old 08-01-2022 , 18:57   Re: What IS The Simplest Way To Pause Timer
Reply With Quote #5

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.
__________________
@.boink. on Discord | DM for Plugin Requests | Tip Jar

Last edited by boink; 08-01-2022 at 18:57.
boink is offline
alasfourom
Senior Member
Join Date: Feb 2022
Location: Saudi Arabia
Old 08-01-2022 , 23:38   Re: What IS The Simplest Way To Pause Timer
Reply With Quote #6

Got it, thank you
alasfourom is offline
Earendil
Senior Member
Join Date: Jan 2020
Location: Spain
Old 08-02-2022 , 13:24   Re: What IS The Simplest Way To Pause Timer
Reply With Quote #7

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

__________________
>>My plugins<<
>>GitHub<<
Earendil is offline
alasfourom
Senior Member
Join Date: Feb 2022
Location: Saudi Arabia
Old 08-02-2022 , 14:24   Re: What IS The Simplest Way To Pause Timer
Reply With Quote #8

Thats more than clear now Earendil, Thank you very much as always
alasfourom is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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