AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   Timers (https://forums.alliedmods.net/showthread.php?t=42662)

johnjg75 08-04-2006 23:59

Timers
 
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.
Code:
#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.
Code:
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 so
Pretty simple, eh?

Freecode 08-05-2006 14:48

Re: Timers
 
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

Re: Timers
 
Quote:

Originally Posted by Freecode
1. Might actualy use the actual team #'s. As enum's default first value is 0.

Done :D
Quote:

Originally Posted by Freecode
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

Re: Timers
 
This would be better done with entity thinks rather than a repeatitive set_task.


All times are GMT -4. The time now is 22:23.

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