PDA

View Full Version : [Question] KillTimer


SamuraiBarbi
09-09-2007, 23:29
Hey there guys. I've been having problems trying to kill timers and was hoping someone here might know what I'm probably doing incorrectly. This script borrows the idea from an evenscripts script that emits a normal heartbeat from a player while their health is above a certain number. In this case it is 30. Then once their health is at or lower than 30 the user player begins to lose health and they emit a faster paced heartbeat until their health reaches 1. I've been looking over the information available for the killtimer function but I'm silly and dun get it yet.


#pragma semicolon 1

#include <sourcemod>
#include <sdktools>


new Handle:hHeartBeatTimer[65];

public OnPluginStart()
{

HookEvent("player_spawn", HookPlayerSpawn);
HookEvent("round_end", HookRoundEnd);
HookEvent("player_death", HookPlayerDeath);
}


public Action:HookPlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
new userID = GetEventInt(event,"userid");
new user = GetClientOfUserId(userID);
new userTeam = GetClientTeam(user);

if (userTeam == 3)
{
hHeartBeatTimer[64] = CreateTimer(1.0, HeartBeat, user);
}
}


public Action:HookRoundEnd(Handle:event, const String:name[], bool:dontBroadcast)
{

new playersconnected;
playersconnected = GetMaxClients();
for (new i = 1; i <= playersconnected; i++)
{
if(IsClientInGame(i))
{
new clientTeam = GetClientTeam(i);

if (clientTeam == 3)
{
if (IsValidHandle(hHeartBeatTimer[i]))
{
PrintToChatAll("killed timer for %i",i);
KillTimer(hHeartBeatTimer[i]);
hHeartBeatTimer[i] = INVALID_HANDLE;
}
}
}
}
}



public Action:HeartBeat(Handle:timer, any:user)
{
if(IsClientInGame(user))
{
new userTeam = GetClientTeam(user);
if (userTeam == 3)
{
decl String:sound[64];
new health = GetClientHealth(user);
if (health <= 30)
{

CreateTimer(1.0, HeartBeat, user);

if (health > 1)
{
Format(sound,sizeof(sound),"km_gameplay_sounds/fastbeat.mp3");
EmitSoundToAll(sound,user);
SetEntProp(user,Prop_Send,"m_iHealth",GetClientHealth(user)-1);

}
}
if (health > 30)
{
Format(sound,sizeof(sound),"km_gameplay_sounds/slowbeat.mp3");
EmitSoundToAll(sound,user);
CreateTimer(1.0, HeartBeat, user);
}

PrintToChatAll("Do heartbeat");
}
}
}


public Action:HookPlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
{

new userid = GetEventInt(event,"userid");
new victim = GetClientOfUserId(userid);
new victimTeam = GetClientTeam(victim);

if ((victimTeam == 3))
{

if (IsValidHandle(hHeartBeatTimer[victim]))
{
PrintToChatAll("killed timer for %i",victim);
KillTimer(hHeartBeatTimer[victim]);
hHeartBeatTimer[victim] = INVALID_HANDLE;
}
}
}

dalto
09-09-2007, 23:37
It is probably because you are explicitly using index 64 in playerspawn instead of user.

SamuraiBarbi
09-09-2007, 23:50
*slaps self*

thanks again dalto, you're a life saver. that was exactly what i needed to fix the problem. :D

sumguy14
09-11-2007, 18:54
Just a tip, KillTimer is really only needed if you need to do more than just kill the timer (doesn't sound right, I know lol)

You can just do CloseHandle(hHeartBeatTimer[i]);

I presume that might be a tad quicker, but it's totally optional, what you have is fine too.

SamuraiBarbi
09-11-2007, 19:03
Ah! Thanks for the tip. I've been learning so much from you guys over the last few days. Everyone here has been so helpful too, which I definitely appreciate. Looking forward to giving something back by releasing a plugin sometime. :)