Make event.final_health can be negative in on_entity_damaged event.

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
x2605
Inserter
Inserter
Posts: 41
Joined: Fri Jul 31, 2020 4:08 pm
Contact:

Make event.final_health can be negative in on_entity_damaged event.

Post by x2605 »

I'm making vanilla multiplay scenario. (really don't want people(including me) to restart entire factorio when switching between servers or their own saved games, so i'm making it in vanilla)
Due to api doesn't support modifying resistance or max_health parameters(locked in prototypes),
I'm relying on on_entity_damaged event. But I can't get accurate last HP before an entity die.

I have read these articles,

Add on_pre_entity_damaged event [2018] - Won't implement
viewtopic.php?f=221&t=63529
[Lua] Event on_pre_entity_damaged [2019] - Won't implement
viewtopic.php?f=221&t=69648
Access to original health/current lifetime [2020] -
viewtopic.php?p=484320#p484320

I think they are saying about similar function.
But I guess developers want to preserve current state due to performance reason.

Then how about to remove negative detection?
I think health related values have 'float' types so it can have negative values, unless you made it as some custom 'ufloat' type.
I found that event.final_health is definetly useless because it always same to event.entity.health.
I think both are always accessible. So event.final_health is useless.
But it will get meaning if it can have negative value, allowing such simple calculation always accurate :

Code: Select all

(LastHP just before damaged) = event.final_health + event.final_damage_amount
Currently it's not accurate when on_entity_damaged is called when entity is about to die,
because both event.entity.health and event.final_health are 0
but event.final_damage_amount have pure damage calculated from original damage w/o health bottom limit(zero).
Which means there is no way to get entity's last health before damaged in this case.
English is not my native language. Sorry for bad English.
My mods :
Lua API global Variable Viewer (mod portal)
Lua API Event Trace (mod portal)
My tools :
Locale String Editor (github.io webapp)

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

Re: Make event.final_health can be negative in on_entity_damaged event.

Post by AartBluestoke »

Note: this is only the 1 case where applying damage would reduce health to 0 that is causing issues. There needs to be one of 3 things:
1) we need to know the entities life before the hit as a parameter.
2) the total damage needs to list the actual damage applied (but that could complicate things if there are multiple damage listeners)
3) the end health needs to go negative, even if the enties health goes 0 - this should be a simple code change, where it is currently calc_damage();set_floor0();trigger_mod_callback;
we just want that to be calc_damage();trigger_mod_callback(); set damage floor()


to work around this you have to have an on-tick event to keep track of all players health yourself, and use that when the resulting health is 0.
You also need to be careful about multiple hits in the same tick, so you can't just refer to that tracked health, you need to update it for every on_damage event applied to a player.

because each mod is independent, you would have to keep this list for your mod, and i would for the mod i contribute to, as would every other mod that wants to interact with (protect from or apply) damage


It seems that wanting to change how damage is applied is a mod feature with a common use case, it is surprising that you can't interact with this without resorting to complex issues.

This affects any mod that is looking to modify
- shields, armors, friendly fire, wierd damage types

also
Is it possible to alter an on_entity_damaged event?
Re: Simple Questions and Short Answers
Modding question - Equipment to add resistances?

Re: How can I modify player damage?

which concludes "
In each case I'll need to calculate what I think the extra damage should be and adjust the target's HP accordingly and then pray this doesn't break interaction with other mods because I don't believe I can update the final_damage_amount passed to them.

I might ultimately have to go this route, but I was wondering if there was a simpler or safer alternative. Especially since there are a number of forum posts from Rseding saying this event is a performance risk."

Given that there are probably already multiple mods which are having to do compicated and risky workarounds for this, could we please just have an extra parameter on the call which tells us the original life, so that in the case where the entity is about to die, we can do something sensible?

Post Reply

Return to “Modding interface requests”