Is it possible to alter an on_entity_damaged event?

Place to get help with not working mods / modding interface.
Post Reply
AngledLuffa
Fast Inserter
Fast Inserter
Posts: 183
Joined: Fri Jan 05, 2018 5:18 pm
Contact:

Is it possible to alter an on_entity_damaged event?

Post by AngledLuffa »

Is there a way to intercept an on_entity_damaged event and change the amount of damage being dealt? I'm exploring various ways to implement a shield research mod to improve the strength of a shield, and one of the ideas I'm thinking of now is to change how much damage is dealt for an on_entity_damaged event. It's pretty clear that altering the number directly doesn't do it. I tried this, and getting hit by a train still hurt:

script.on_event(defines.events.on_entity_damaged,function(event)
event.final_damage_amount = 0
end)

Solinya
Long Handed Inserter
Long Handed Inserter
Posts: 77
Joined: Sun Mar 17, 2019 10:39 pm
Contact:

Re: Is it possible to alter an on_entity_damaged event?

Post by Solinya »

Depending on what you're trying to do, you can modify the damaged entity's health instead, to either undo or increase the damage. E.g. to negate the damage, just add back event.final_damage_amount to the entity's health:

Code: Select all

event.entity.health = event.entity.health + event.final_damage_amount
It's not a fully comprehensive solution as I don't think you have access to the entity's resistances, so if you want a %-based reduction, you'll have to store the % amount to reduce separately.

AngledLuffa
Fast Inserter
Fast Inserter
Posts: 183
Joined: Fri Jan 05, 2018 5:18 pm
Contact:

Re: Is it possible to alter an on_entity_damaged event?

Post by AngledLuffa »

This is certainly a possibility. It has some difficulties, though. You can check the entity's grid to see if it has any shields equipped. From there, you can see if the shields have reached 0, which suggests the character just took damage. Unless you know what the starting shields were, though, you don't know how much of the damage should go back to health vs how much should go back to the shields. It's possible that you look at a character who just took 200 damage and has 0 shields and 100 hit points lost. They might have started with 100 shields and 0 damage, or they might have started with 190 shields and 90 hit points already lost. Not sure how to make that distinction.

AngledLuffa
Fast Inserter
Fast Inserter
Posts: 183
Joined: Fri Jan 05, 2018 5:18 pm
Contact:

Re: Is it possible to alter an on_entity_damaged event?

Post by AngledLuffa »

To be clear, I'm looking to make a research where shields are more durable. So if they currently have a 10% bonus, the character in my example who started with 100 shields left would have soaked 110 damage with shields and only taken 90 hit points, whereas the character with 190 shields left would have soaked all 200 and still have ~10 shields left.

AartBluestoke
Inserter
Inserter
Posts: 31
Joined: Sun Jun 29, 2014 4:50 am
Contact:

Re: Is it possible to alter an on_entity_damaged event?

Post by AartBluestoke »

Solinya wrote:
Sun Feb 02, 2020 3:16 am
Depending on what you're trying to do, you can modify the damaged entity's health instead, to either undo or increase the damage. E.g. to negate the damage, just add back event.final_damage_amount to the entity's health:

Code: Select all

event.entity.health = event.entity.health + event.final_damage_amount
It's not a fully comprehensive solution as I don't think you have access to the entity's resistances, so if you want a %-based reduction, you'll have to store the % amount to reduce separately.
Don't Do that!
if the person health would be 0, you will heal them if they take a single big hit.
if you have 1 hitpoint left, and you take 1000 damage the above formula would leave you on 1000 life, not 1.

There is currently no way to do this without tracking the players health yourself.

Post Reply

Return to “Modding help”