Raised This Month: $32 Target: $400
 8% 

Timer duplication?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
jcarle
Junior Member
Join Date: Jan 2008
Location: Joliette, QC, Canada
Old 01-27-2008 , 18:53   Timer duplication?
Reply With Quote #1

I wrote the following clock script :
PHP Code:
#include <sourcemod>
#define PLUGIN_VERSION "1.0.0"
 
new Handle:CLKEnabled;
new 
Handle:TimeZone;
new 
Handle:cTimer;
 
public 
Plugin:myinfo 
{
 
name "Clock",
 
author "Jean-Sebastien Carle",
 
description "Displays the system time to all clients.",
 
version PLUGIN_VERSION,
 
url "http://www.sourcemod.net/"
}
 
public 
OnPluginStart()
{
 
CreateConVar("clk_version"PLUGIN_VERSION"Clock Version"FCVAR_PLUGIN FCVAR_SPONLY FCVAR_REPLICATED FCVAR_NOTIFY);
 
CLKEnabled CreateConVar("clk_enabled""1""Enable plugin");
 
TimeZone CreateConVar("clk_timezone""GMT-5""Timezone");
 
AutoExecConfig(true"plugin.clock""sourcemod");
}
 
public 
OnConfigsExecuted()
{
 
PrintToServer("[Clock] Plugin loaded.");
 
HookConVarChange(CLKEnabledOnConvarChanged);
 if (
GetConVarInt(CLKEnabled) == 1)
  
cTimer CreateTimer(60.0PrintTime_TIMER_REPEAT);
}
 
public 
OnConvarChanged(Handle:convar, const String:oldValue[], const String:newValue[])
{
    static 
iNewVal 0;
    if (
convar == CLKEnabled)
    {
        
iNewVal StringToInt(newValue);
        if (
StringToInt(oldValue) != iNewVal)
            if (
iNewVal 0) {
    
cTimer CreateTimer(60.0PrintTime_TIMER_REPEAT);
            } else {
    
KillTimer(cTimerfalse);
            }
    }
}
 
public 
OnPluginEnd()
{
 
KillTimer(cTimerfalse); 
}
 
public 
Action:PrintTime(Handle:timer)
{
 
decl String:curTimeZone[10];
 
decl String:curTime[20];
 
 
GetConVarString(TimeZonecurTimeZonesizeof(curTimeZone));
 
FormatTime(curTimesizeof(curTime), "%I:%M %p"GetTime());
 if (
StrContains(curTime":00"false) >= || StrContains(curTime":15"false) >= || StrContains(curTime":30"false) >= || StrContains(curTime":45"false) >= 0)
 {
  
PrintHintTextToAll("%s %s"curTimecurTimeZone);
  
PrintToServer("%s %s"curTimecurTimeZone);
 }
 
 return 
Plugin_Continue;

For some reason, sometimes, it seems like the timers double up and I end up getting the time displayed multiple times in a row...
jcarle is offline
teame06
i have a hat
Join Date: Feb 2005
Location: Hat City
Old 01-28-2008 , 01:05   Re: Timer duplication?
Reply With Quote #2

public OnPluginEnd() << Is called when the plugin is unloaded or reloaded due to file changes. Any timers when plugins are reloaded or unloaded are killed.

If the plugin does not get reloaded or unloaded during map changes. The timer will carry over to the next map.

You would have to use the forward OnMapEnd(); to kill the timer or use the flag "TIMER_FLAG_NO_MAPCHANGE".

Code:
public OnMapEnd() {     if(cTimer != INVALID_HANDLE)     {         KillTimer(cTimer, false);         cTimer = INVALID_HANDLE;     } }

If you used the TIMER_FLAG_NO_MAPCHANGE. Then you would still need to do that.

Code:
public OnMapEnd() {     cTimer = INVALID_HANDLE; }

In your OnConvarChanged forward Handle. Your not checking if cTimer != INVALID_HANDLE; If it a valid handle that already has a repeated timer. Your just creating another one on top of that one.

This is what I would do in that part.

Code:
if (iNewVal > 0) {     if(cTimer != INVALID_HANDLE)     {         KillTimer(cTimer, false);         cTimer = INVALID_HANDLE;     }         cTimer = CreateTimer(60.0, PrintTime, _, TIMER_REPEAT); } else {     KillTimer(cTimer, false);     cTimer = INVALID_HANDLE; }
__________________
No private support via Instant Message
GunGame:SM Released

Last edited by teame06; 01-28-2008 at 01:15.
teame06 is offline
Send a message via AIM to teame06
jcarle
Junior Member
Join Date: Jan 2008
Location: Joliette, QC, Canada
Old 01-28-2008 , 05:01   Re: Timer duplication?
Reply With Quote #3

The problem is not that it's stopping at the end of the map, it's that the timers are accumulating. Or are you saying that my problem is because I'm not stopping the timer at the end of the map because timers get restarted with map changes? or ? ...
jcarle is offline
ferret
SourceMod Developer
Join Date: Dec 2004
Location: Atlanta, GA
Old 01-28-2008 , 10:31   Re: Timer duplication?
Reply With Quote #4

You start a timer every map. You don't ever kill it. Timers do NOT stop when the map ends. They continue to the next map.

So every map you add a new one, and don't kill the old one.
__________________
I'm a blast from the past!
ferret is offline
jcarle
Junior Member
Join Date: Jan 2008
Location: Joliette, QC, Canada
Old 01-28-2008 , 16:26   Re: Timer duplication?
Reply With Quote #5

Where is the timer being added? Does OnPluginStart() get called every map change?

Last edited by jcarle; 01-28-2008 at 16:35.
jcarle is offline
bl4nk
SourceMod Developer
Join Date: Jul 2007
Old 01-28-2008 , 17:07   Re: Timer duplication?
Reply With Quote #6

OnConfigsExecuted() probably.
bl4nk is offline
jcarle
Junior Member
Join Date: Jan 2008
Location: Joliette, QC, Canada
Old 01-29-2008 , 10:33   Re: Timer duplication?
Reply With Quote #7

Assuming that the order in which events are fired is the following :

1] OnPluginStart()
2] OnMapStart()
3] OnGameFrame()
4] OnMapEnd()
5] OnPluginEnd()

After OnMapEnd() is called, the next map that loads would call OnPluginStart() and start the cycle from the top again? And from a previous in this thread OnPluginEnd() only gets called when the plugin is refreshed or unloaded completely?
jcarle is offline
bl4nk
SourceMod Developer
Join Date: Jul 2007
Old 01-29-2008 , 11:37   Re: Timer duplication?
Reply With Quote #8

Well you're not starting a timer in OnPluginStart(), you're creating it in OnConfigsExecuted() and OnConVarChange() for your enable cvar.
bl4nk is offline
ferret
SourceMod Developer
Join Date: Dec 2004
Location: Atlanta, GA
Old 01-29-2008 , 13:51   Re: Timer duplication?
Reply With Quote #9

Event flow:

#1) Server starts and loads plugins: OnPluginStart() (Once per plugin load)

#2) Map Start: OnMapStart() (Every map)

#3) Configs executed for map: OnConfigsExectued() (Every map, after OnMapStart)

#4) OnGameFrame() Every single frame, can occur at any point where game frames are running, including before or after OnConfigsExecuted and other forwards

#5) OnMapEnd() - called at the end of every map

#6) OnPluginEnd() - Called ONLY when the plugin is unloaded at server shutdown, or if the plugin is being reloaded because it changed. It is NOT called on map end.
__________________
I'm a blast from the past!
ferret is offline
jcarle
Junior Member
Join Date: Jan 2008
Location: Joliette, QC, Canada
Old 01-30-2008 , 01:01   Re: Timer duplication?
Reply With Quote #10

Thanks ferret, now I'm starting to make sense of this all.

Considering that my timer is created based on the value of a cvar, then I was correct in placing my code for the timer creation within OnConfigsExecuted. It would appear, however, that my mistake was to put the timer destruction code in OnPluginEnd.

Confirm me if I'm right or correct me if I'm wrong. My solutions would be to either a) move the timer destruction code to OnMapEnd or b) check to see if a timer already exists in OnConfigsExecuted before creating it?
jcarle is offline
Reply



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 23:37.


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