PDA

View Full Version : Timers


johnjg75
08-04-2006, 23:59
I'm currently making a TKOTH plugin that uses a timer and i figured that I'd share the timer code because some people need help with timers. This example uses a small struct for teams and it shows how easy OOP can be.#include <amxmodx>
#include <cstrike>

/*enum CsTeams {
CS_TEAM_UNASSIGNED = 0,
CS_TEAM_T = 1,
CS_TEAM_CT = 2,
CS_TEAM_SPECTATOR = 3
};*/

new bool:isTimerOn[CsTeams];
new zonetime[CsTeams];
new TimerID[CsTeams];

startTimer(CsTeams:team)
{
if(isTimerOn[team])
return 0;
set_task(1.0,"Tick",TimerID[team],team,sizeof(team),"b");
isTimerOn=true;
}

Tick(CsTeams:team)
{
zonetime[team]++;
}

stopTimer(CsTeams:team)
{
remove_task(TimerID[team]);
isTimerOn=false;
resetTimer(team);
}

resetTimer(CsTeams:team)
{
zonetime[team]=0;
}

pauseTimer(CsTeams:team)
{
remove_task(TimerID[team]);
isTimerOn=false;
}

currentTime(CsTeams:team)
return zonetime[team];Now this is plugin specific, but you can easily modify it to fit your plugin's needs.

Then you'd simply do what you need to do, i.e.new bool:inzone[Team]
docheck();
if(inzone[CS_TEAM_T] && !lastcheck[CS_TEAM_T])// if they are now in the zone and wasn't before
startTimer(CS_TEAM_T);
if(!inzone[CS_TEAM_T] && lastcheck[CS_TEAM_T]) // if they were in the zone but aren't anymore
stopTimer(CS_TEAM_T);
// do same with CT and make an update task every second or soPretty simple, eh?

Freecode
08-05-2006, 14:48
1. Might actualy use the actual team #'s. As enum's default first value is 0.
2. Not really OOP ;] (Since pawn doesnt support OOP)

johnjg75
08-05-2006, 17:19
1. Might actualy use the actual team #'s. As enum's default first value is 0.Done :D
2. Not really OOP ;] (Since pawn doesnt support OOP)yeah, i know but its as close as you can get in pawn :)

Xanimos
08-06-2006, 02:16
This would be better done with entity thinks rather than a repeatitive set_task.