AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Timer duplication? (https://forums.alliedmods.net/showthread.php?t=66298)

jcarle 01-27-2008 18:53

Timer duplication?
 
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...

teame06 01-28-2008 01:05

Re: Timer duplication?
 
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; }

jcarle 01-28-2008 05:01

Re: Timer duplication?
 
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 ? ...

ferret 01-28-2008 10:31

Re: Timer duplication?
 
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.

jcarle 01-28-2008 16:26

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

bl4nk 01-28-2008 17:07

Re: Timer duplication?
 
OnConfigsExecuted() probably.

jcarle 01-29-2008 10:33

Re: Timer duplication?
 
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?

bl4nk 01-29-2008 11:37

Re: Timer duplication?
 
Well you're not starting a timer in OnPluginStart(), you're creating it in OnConfigsExecuted() and OnConVarChange() for your enable cvar.

ferret 01-29-2008 13:51

Re: Timer duplication?
 
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.

jcarle 01-30-2008 01:01

Re: Timer duplication?
 
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?


All times are GMT -4. The time now is 11:34.

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