The problem when you create a custom entity in goldsrc and enable takedamage is
that the engine will automatically kill the entity when it's health is less than zero.
You DO NOT want this to happen, you want to fully control when your
custom entity gets removed. To do this you need to set a health mask with
a high enough value that no weapon/explosive in-game can easily destroy it.
Code:
#define HEALTH_MASK 1000000.0
.
.
entity_set_float( entity_stick, EV_FL_health, HEALTH_MASK );
Then, check the health in some function, usually it's think function or a hooked
Takedamage function and then evaluate whether or not you need to kill the entity.
If you do, then perform whatever code cleanup required and then kill the entity.
Code:
public StickThink( ent )
{
static Float:flHealth;
flHealth = get_pcvar_float( stick_health );
if ( flHealth < 1.0 ) flHealth = 1.0; // idiot proof your HP value
if ( flHealth > 500.0 ) flHealth = 500.0;
if ( entity_get_float( ent, EV_FL_health ) <= (HEALTH_MASK - flHealth) )
{
// do any code cleanup here
//
// new id = entity_get_int( ent, EV_INT_iuser2 );
// if ( SomeIsValidPlayerIDFunction(id) )
// client_print( id, print_chat, "Your stick was destroyed" );
remove_entity( ent );
return PLUGIN_CONTINUE;
}
.
.
.
return PLUGIN_CONTINUE;
}
One last thing: If you use CVARs or load settings from a configuration file, don't
plug these values
directly into your code. You need to
idiot proof these values
before plugging them in, because an invalid setting can sometimes have unpredictable
results, like crashing the server.
__________________