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)
Is it possible to alter an on_entity_damaged event?
-
- Fast Inserter
- Posts: 187
- Joined: Fri Jan 05, 2018 5:18 pm
- Contact:
Re: Is it possible to alter an on_entity_damaged event?
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:
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.
Code: Select all
event.entity.health = event.entity.health + event.final_damage_amount
-
- Fast Inserter
- Posts: 187
- Joined: Fri Jan 05, 2018 5:18 pm
- Contact:
Re: Is it possible to alter an on_entity_damaged event?
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.
-
- Fast Inserter
- Posts: 187
- Joined: Fri Jan 05, 2018 5:18 pm
- Contact:
Re: Is it possible to alter an on_entity_damaged event?
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.
-
- Inserter
- Posts: 36
- Joined: Sun Jun 29, 2014 4:50 am
- Contact:
Re: Is it possible to alter an on_entity_damaged event?
Don't Do that!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:
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.Code: Select all
event.entity.health = event.entity.health + event.final_damage_amount
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.