On_PlayerHurt

Jakkee

Retired Staff
Retired Staff
Plugin Developer
Jul 28, 2014
1,465
932
113
Australia
Method:
On_PlayerHurt

Argument(s):
HurtEvent he

Examples:
Python:
Python:
def On_PlayerHurt(self, HurtEvent):
attacker = HurtEvent.Attacker.Name
victim = HurtEvent.Victim.Name
amount = HurtEvent.DamageAmount
Server.BroadcastFrom("DEATHMSG", attacker + " dealt " + str(amount) + "damage to " + victim)

Javascript:
JavaScript:
function On_PlayerHurt(HurtEvent) {
var attacker = HurtEvent.Attacker.Name;
var victim = HurtEvent.Victim.Name;
var amount = HurtEvent.DamageAmount;
Server.BroadcastFrom("DEATHMSG", attacker + " dealt " + amount + "damage to " + victim);
}
C#:
C#:
public override void Initialize()
{
Hooks.OnPlayerHurt += PlayerHurt;
}

public override void DeInitialize()
{
Hooks.OnPlayerHurt -= PlayerHurt;
}
C#:
public void PlayerHurt(HurtEvent he)
{
// Checking if the Attacker is a Player
if (he.Attacker is Fougerite.Player)
{
Player attacker = (Player) he.Attacker;
Player victim = (Player) he.Victim;

string attackerName = attacker.Name;
string victimName = victim.Name;

float damageAmount = he.DamageAmount;

Server.GetServer().Broadcast(attackerName + " dealt " + damageAmount + " damage to " + victimName);
}
}
 

MasterPeace

Retired Staff
Retired Staff
Feb 2, 2015
269
69
28
45
Poland
Question: How to check if Attacker is Player without checking WeaponName?
Question: What we are receiving with Attacker / Victim?

1. I tried to check SteamID, with len() but it always showing me that Attacker is Player (but it's Large Spike Wall)

2. Is this:
a) player / npc object: Attacker.Name = MyBeautifulName
b) private object of HurtEvent class: Attacker.Name = Player
 

Jakkee

Retired Staff
Retired Staff
Plugin Developer
Jul 28, 2014
1,465
932
113
Australia
Question: How to check if Attacker is Player without checking WeaponName?
Question: What we are receiving with Attacker / Victim?

1. I tried to check SteamID, with len() but it always showing me that Attacker is Player (but it's Large Spike Wall)

2. Is this:
a) player / npc object: Attacker.Name = MyBeautifulName
b) private object of HurtEvent class: Attacker.Name = Player
HurtEvent.Attacker is an object.
By adding .Name you'll get the name of the attacker, If you add .SteamID you'll get the SteamID of the attacker.
But getting the SteamID of an attacker would throw an error if its an NPC attacking a player.
So you'd do an if statement to see if the attackers SteamID is None.
( if HurtEvent.Attacker.SteamID is None: Return)
Also adding a Try/Except would help.

An object could be a Player, Entity or an NPC.

Hope that helped
 

Snake

Moderator
Moderator
Jul 13, 2014
288
174
28
HurtEvent.Attacker is an object.
By adding .Name you'll get the name of the attacker, If you add .SteamID you'll get the SteamID of the attacker.
But getting the SteamID of an attacker would throw an error if its an NPC attacking a player.
So you'd do an if statement to see if the attackers SteamID is None.
( if HurtEvent.Attacker.SteamID is None: Return)
Also adding a Try/Except would help.

An object could be a Player, Entity or an NPC.

Hope that helped
I used to do that but handling exceptions and this stuff is very bad.

Now, what I use is (in C#) :

C#:
if (HurtEvent.Attacker is Fougerite.Player)
{
     // CODE HERE
}
This is way faster and easier to read.
 

Jakkee

Retired Staff
Retired Staff
Plugin Developer
Jul 28, 2014
1,465
932
113
Australia
I used to do that but handling exceptions and this stuff is very bad.

Now, what I use is (in C#) :

C#:
if (HurtEvent.Attacker is Fougerite.Player)
{
     // CODE HERE
}
This is way faster and easier to read.
In Python I believe that would translate to:
Python:
if HurtEvent.Attacker is Server.Players:
#Do stuff here
else:
    Server.Broadcast("Not a player")
 

Snake

Moderator
Moderator
Jul 13, 2014
288
174
28
In Python I believe that would translate to:
Python:
if HurtEvent.Attacker is Server.Players:
#Do stuff here
else:
    Server.Broadcast("Not a player")
Mmm I think that wouldn't work.

Server.Players is a list of the current players.

Fougerite.Player is a type.

With "is" you are checking if the type of the object is the type of whatever you want.
 

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,093
4,784
113
At your house.
github.com
Last edited:

Snake

Moderator
Moderator
Jul 13, 2014
288
174
28
None cool enough.


https://github.com/dretax/Python-Plugins/blob/master/DeathMSG/DeathMSG.py#L307

You can do the same. if "Player" in str(HurtEvent.Attacker)

Another solution:

https://github.com/dretax/Python-Plugins/blob/master/DeathMSG/DeathMSG.py#L290

You can try to grab the entity's steamid. Will return None if its not a player.

Third option:

from Fougerite import Player as PlayerClass

if HurtEvent.Attacker is PlayerClass:
.....
That requires more code than just writing if blabla is Player
 

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,093
4,784
113
At your house.
github.com
That requires more code than just writing if blabla is Player
I don't think "Player" in str(HurtEvent.Attacker) would take more code.

Python, neither Js don't have the Objects in global. It's an interpreter, and those values are not in scope.
 
Last edited:

Snake

Moderator
Moderator
Jul 13, 2014
288
174
28
I don't think "Player" in str(HurtEvent.Attacker) would take more code.

Python, neither Js don't have the Objects in global. It's an interpreter, and those values are not in scope.
Well in case you are not programming in C# then no problem.