Page 1 of 1

Callin order: gadget:UnitPreDamaged and script.HitByWeapon

Posted: 31 Aug 2013, 21:21
by knorke
tl;dr: swap call order of gadget:UnitPreDamaged and script.HitByWeapon


gadget:UnitPreDamaged and script.HitByWeapon are both called when a unit takes damage (and both can overwrite the damage)

example usage would be to use script.HitByWeapon to have the unitscript play a hit-animation when a unit was damaged.

And UnitPreDamaged to overwrite damage: like a unit does not take damage in "superpower mode" or whatever.

But that example already fails, because script.HitByWeapon gets called BEFORE gadget:UnitPreDamaged.
So the unit script would play the hit animation even if the gadget later decides no damage was taken.

For example to make flying debris pieces cause less damage one can do:

Code: Select all

function gadget:UnitPreDamaged (...) 
	if weaponDefID == -1 then -- -1= flying debris pieces
		return 5
But then also needs:

Code: Select all

function script.HitByWeapon (x, z, weaponDefID, damage)	
	if weaponDefID == -1 then damage=5 end - --1= flying debris pieces
...
animation that takes into account damage
...
return damage
Now image multiple gadgets who overwrite damage, some things you can not even detect in unitscript to copy it. (like attacker team)
Share this if you cry everywhere. 1 like = #yoloswag.


I think the order makes more sense like this:
1) gadget:UnitPreDamaged (can overwrite engine damage)
2) script.HitByWeapon (can again overwrite)
3) gadget:UnitDamaged (just notification callin)

I can not think of a situation where you would want 1&2 otherway around.

especially since it also is:
1) gadget:UnitDestroyed
2) script.killed

Or make new LUS callin so that order would be like:
1) script.HitByWeapon (can overwrite engine damage)
2) gadget:UnitPreDamaged (can overwrite above)
3) script.HitByWeaponAfterGadget (just notification callin like UnitDamaged is)
(maybe name it something more clever)
4) gadget:UnitDamaged (just notification callin)

Only other idea I thought of is make your own script.HitByWeaponForReal like:

Code: Select all

gadget:UnitPreDamaged (...)
CallAsUnit (unitID, evn.HitByWeaponForReal, realDamage)
Should I just do that? Or is many CallAsUnit = bad?
Or maybe the swapping can be done with gadget handler already?

Re: Callin order: gadget:UnitPreDamaged and script.HitByWea

Posted: 01 Sep 2013, 12:29
by Silentwings
Personally I'm tempted by the idea of scripts not being able to modify damages at all, only lua. Of the ideas above I'd also go with the first one.

Re: Callin order: gadget:UnitPreDamaged and script.HitByWea

Posted: 01 Sep 2013, 17:45
by CarRepairer
Silentwings wrote:Personally I'm tempted by the idea of scripts not being able to modify damages at all, only lua. Of the ideas above I'd also go with the first one.
Huh? Why does it bother you that other people make use of a feature? I have several units with unique damage logic. Why should I create a new gadget for each one when a script function is more handy?

Knorke's explanation makes perfect sense to me. I use these features a lot.

Re: Callin order: gadget:UnitPreDamaged and script.HitByWea

Posted: 01 Sep 2013, 18:02
by FLOZi
CarRepairer wrote:
Silentwings wrote:Personally I'm tempted by the idea of scripts not being able to modify damages at all, only lua. Of the ideas above I'd also go with the first one.
Huh? Why does it bother you that other people make use of a feature? I have several units with unique damage logic. Why should I create a new gadget for each one when a script function is more handy?

Knorke's explanation makes perfect sense to me. I use these features a lot.
+1

Re: Callin order: gadget:UnitPreDamaged and script.HitByWea

Posted: 01 Sep 2013, 18:05
by Silentwings
Huh? Why does it bother you that other people make use of a feature?
What bothers me is the general issue of being able to overwrite the same thing twice on the same frame from two very different places and, worse, it not being obvious which takes precedence without reading either this thread or engine source.

But, in answer to your ill-posed question, it doesn't and how do you know I've not used it? wtf...

By the way, it happened in https://github.com/spring/spring/commit ... 275ad8f65d.

Re: Callin order: gadget:UnitPreDamaged and script.HitByWea

Posted: 01 Sep 2013, 18:11
by CarRepairer
I'm not sure how else to interpret what you said. If you also use it why would you want the feature gone?

As knorke pointed out there's also gadget unitdestroyed and script unitkilled and there's no obvious order there either but both functions are widely used.

Re: Callin order: gadget:UnitPreDamaged and script.HitByWea

Posted: 01 Sep 2013, 18:12
by Silentwings
Because I've no objection to updating my code if it helps make the api more sensible.

Re: Callin order: gadget:UnitPreDamaged and script.HitByWea

Posted: 01 Sep 2013, 18:45
by CarRepairer
Gadget unitpredamaged applies to all units so it's general.

"Any unit hit by this fireball takes no damage if it's standing in the water"

Script hitbyweaponid applies to a unit type and more specifically this unit so it should be checked afterwards (agree with knorke).

"This derp is being hit by a fireball. Check if its protective shield is moved up at this moment in time. If it is, take half damage. If not, take the full damage (both of which may be zero based on the gadget above)"

Re: Callin order: gadget:UnitPreDamaged and script.HitByWea

Posted: 01 Sep 2013, 18:48
by Silentwings
Gadget unitpredamaged applies to all units so it's general.
It supplies unitID, unitDefID, attackerID, projectileID, etc etc - it's only general when you want it to be.

I completely agree that if you have to have both things then script should come second, as it now does.

Re: Callin order: gadget:UnitPreDamaged and script.HitByWea

Posted: 01 Sep 2013, 19:15
by CarRepairer
Okay, unitpredamaged starts general and then you can whittle it down.

Hitbyweaponid starts at the unitdef/unit level and you can whittle it down.
Silentwings wrote:as it now does.
That's not what knorke says in this thread.

Re: Callin order: gadget:UnitPreDamaged and script.HitByWea

Posted: 01 Sep 2013, 19:40
by Silentwings
Knorke's post is a day old ;)
silentwings wrote:... it happened in https://github.com/spring/spring/commit ... 275ad8f65d

Re: Callin order: gadget:UnitPreDamaged and script.HitByWea

Posted: 02 Sep 2013, 21:06
by knorke
my thread brought a change :shock:
thanks obama.