If you find my old posts, some of those sure have typos or errors what not necessary work,
so
understand what code do rather than copy/paste it blindly.
I'm not great coder, I had just tried practise and looking old codes from here forum posts and plugins.
You need to beware when using timer flag
TIMER_FLAG_NO_MAPCHANGE
and storing timer handle in array.
What can happen is, running timer will "disappear" on map change but you have still handle of that timer what not exist anymore.
- You KillTimer with that not exist timer handle, error.
But if you not get those errors, good.
About checking time left in timer, again, make variable array and store time etc. etc.
Here is my
example of cooldown and with time check.
- I want separate array index and client userid when creating timer. So I know sure what array index I am following.
But you do your own way, it is your plugin not mine
Spoiler
PHP Code:
// sugar coating code
enum struct TimerInfo
{
Handle timer;
float endtime;
float spam_endtime;
void Clear()
{
this.timer = null;
this.endtime = 0.0;
this.spam_endtime = 0.0;
}
}
TimerInfo cooldown[MAXPLAYERS+1];
public void OnPluginStart()
{
RegConsoleCmd("sm_test", test);
}
public Action test(int client, int args)
{
if(!client || !IsClientInGame(client) ||
IsFakeClient(client) || GetClientTeam(client) < 2 || !IsPlayerAlive(client))
{
return Plugin_Handled;
}
float gametime = GetGameTime();
// cooldown in progress
if(cooldown[client].timer != null)
{
if(cooldown[client].spam_endtime < gametime) // reduce chat spam
{
PrintToChat(client, "Cooldown takes %0.1f seconds", cooldown[client].endtime - gametime);
cooldown[client].spam_endtime = gametime + 1.0;
}
return Plugin_Handled;
}
// create cooldown
DataPack pack;
cooldown[client].timer = CreateDataTimer(30.0, coolio, pack);
cooldown[client].endtime = GetGameTime() + 30.0;
pack.WriteCell(client); // This will be array index
pack.WriteCell(GetClientUserId(client)); // With this we find this same client later (or not)
// sprint code here
PrintToChat(client, "Sprint!");
return Plugin_Handled;
}
public Action coolio(Handle timer, DataPack pack)
{
pack.Reset();
int index = pack.ReadCell(); // array index
// if some reason timer differ from stored handle, stop here and do nothing
// - there is new timer created or handle cleared ? Doesn't matter
if(cooldown[index].timer != timer)
return Plugin_Continue;
//cooldown[index].timer = null;
//cooldown[index].endtime = 0.0; // It's game time, when new map start, game time start count from 0.0
//cooldown[index].spam_endtime = 0.0;
cooldown[index].Clear(); // This do same as those 3 above lines.
int client = GetClientOfUserId(pack.ReadCell()); // userid
// client not found or not in game in this moment
if(!client || !IsClientInGame(client))
return Plugin_Continue;
PrintToChat(client, "Cooldown off!");
return Plugin_Continue;
}
__________________