Raised This Month: $32 Target: $400
 8% 

Solved Detect if an NPC died with SDKHook?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Ethorbit
Member
Join Date: Sep 2016
Location: Oregon
Old 01-03-2018 , 23:57   Detect if an NPC died with SDKHook?
Reply With Quote #1

I know you can see if a player died, but how would you know if an NPC died? I've been looking everywhere, I cannot find a way.

I don't want to use OnEntityDestroyed as I cannot use SDKHook_OnTakeDamagePost with it to see who killed it.


Edit: Thanks again psychonic!

PHP Code:
public OnEntityCreated(entity

HookEvent("entity_killed"EntityKilledEventHookMode_Post);
}

public 
OnEntityDestroyed(entity

UnhookEvent("entity_killed"EntityKilledEventHookMode_Post);
}

public 
EntityKilled(Handle:event, const String:name[], bool:Broadcast
{      
    new 
killed GetEventInt(event"entindex_killed")        
    if(
killed){
        new 
String:classname[32];
        new 
String:attackername[32];
        
GetEntityClassname(killedclassnamesizeof(classname));
        
PrintToChatAll("The npc that died: %s"classname);
        new 
attacker GetEventInt(event"entindex_attacker")
        if(
IsValidClient(attacker)){
            
GetClientName(attackerattackernamesizeof(attackername))
            
PrintToChatAll("It died by: %s"attackername);
        }
    }
}

stock bool:IsValidClient(clientbool:nobots true)

    if (
client <= || client MaxClients || !IsClientConnected(client) || (nobots && IsFakeClient(client)))
    {
        return 
false
    }
    return 
IsClientInGame(client); 


Last edited by Ethorbit; 01-05-2018 at 23:53.
Ethorbit is offline
cravenge
Veteran Member
Join Date: Nov 2015
Location: Chocolate Factory
Old 01-04-2018 , 18:51   Re: [Question] Detect if an NPC died with SDKHook?
Reply With Quote #2

Game?
cravenge is offline
Ethorbit
Member
Join Date: Sep 2016
Location: Oregon
Old 01-04-2018 , 23:13   Re: [Question] Detect if an NPC died with SDKHook?
Reply With Quote #3

Quote:
Originally Posted by cravenge View Post
Game?
Half-Life 2: Deathmatch (I don't think the game matters), although I am able to detect the health of the npc every time it is hit, my only issue right now is that it's returning very inaccurate numbers:

PHP Code:
public OnEntityCreated(entity

SDKHook(entitySDKHook_OnTakeDamageOnTakeDamage); 
}

public 
OnTakeDamage(victimattackerinflictorFloat:damagedamagetype
{  
    if(
IsValidEntity(victim)){    
        
int entvictimhealth
        entvictimhealth 
GetEntProp(victimProp_Data"m_iHealth");
        
PrintToChatAll("Ent's health: %i"entvictimhealth);
    }

It says Ent's health: 10 or 2 when the npc dies, it doesn't say 0 like it should. My guess is that it returns the last health it has before it dies.

Last edited by Ethorbit; 01-04-2018 at 23:16.
Ethorbit is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 01-05-2018 , 01:04   Re: Detect if an NPC died with SDKHook?
Reply With Quote #4

...it never occurred to you to do this?

PHP Code:
float health entvictimhealth damage;
if (
health <= 0.0)
{
    
// He's dead, Jim

__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 01-05-2018 at 01:04.
Powerlord is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 01-05-2018 , 01:36   Re: Detect if an NPC died with SDKHook?
Reply With Quote #5

Quote:
Originally Posted by Powerlord View Post
...it never occurred to you to do this?

PHP Code:
float health entvictimhealth damage;
if (
health <= 0.0)
{
    
// He's dead, Jim

This is pretty much what I do in Tf2 for tf_zombie entities. Worked out so far.
Mitchell is offline
Ethorbit
Member
Join Date: Sep 2016
Location: Oregon
Old 01-05-2018 , 02:40   Re: Detect if an NPC died with SDKHook?
Reply With Quote #6

Quote:
Originally Posted by Powerlord View Post
...it never occurred to you to do this?

PHP Code:
float health entvictimhealth damage;
if (
health <= 0.0)
{
    
// He's dead, Jim

No, it did not, and it's giving me the same result.

PHP Code:
public OnEntityCreated(entity

SDKHook(entitySDKHook_OnTakeDamageOnTakeDamage); 
}

public 
OnTakeDamage(victimattackerinflictorFloat:damagedamagetype
{  
    if(
IsValidEntity(victim)){
    
int entvictimhealth
    entvictimhealth 
GetEntProp(victimProp_Data"m_iHealth");
    
float health entvictimhealth damage;
    
PrintToChatAll("The ent's health: %f"health); // Never prints 0.0
        
if (health <= 0.0)
        {
            
PrintToChatAll("It's dead!"); // Never prints.
        
}
    }

Here's what it looks like in-game for me:
https://www.youtube.com/watch?v=BOlDyQYZUFo

I appreciate the feedback.
Ethorbit is offline
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 01-05-2018 , 02:49   Re: Detect if an NPC died with SDKHook?
Reply With Quote #7

try
PHP Code:
if (damage entvictimhealthPrintToChatAll("It's dead!"); 
__________________
8guawong is offline
psychonic

BAFFLED
Join Date: May 2008
Old 01-05-2018 , 07:59   Re: Detect if an NPC died with SDKHook?
Reply With Quote #8

I believe that most NPCs support the entity_killed event. You could just listen for that.
psychonic is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 01-05-2018 , 10:32   Re: Detect if an NPC died with SDKHook?
Reply With Quote #9

Quote:
Originally Posted by Ethorbit View Post
No, it did not, and it's giving me the same result.

PHP Code:
public OnEntityCreated(entity

SDKHook(entitySDKHook_OnTakeDamageOnTakeDamage); 
}

public 
OnTakeDamage(victimattackerinflictorFloat:damagedamagetype
{  
    if(
IsValidEntity(victim)){
    
int entvictimhealth
    entvictimhealth 
GetEntProp(victimProp_Data"m_iHealth");
    
float health entvictimhealth damage;
    
PrintToChatAll("The ent's health: %f"health); // Never prints 0.0
        
if (health <= 0.0)
        {
            
PrintToChatAll("It's dead!"); // Never prints.
        
}
    }

Here's what it looks like in-game for me:
https://www.youtube.com/watch?v=BOlDyQYZUFo

I appreciate the feedback.
It seems to me that you're comparing a float and an integer, I doubt it works properly.
eyal282 is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 01-05-2018 , 12:36   Re: Detect if an NPC died with SDKHook?
Reply With Quote #10

Operations that mix floats and integers work AFAIK. The int is just implicitly converted to a float.
hmmmmm is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:41.


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