Callin order: gadget:UnitPreDamaged and script.HitByWeapon

Callin order: gadget:UnitPreDamaged and script.HitByWeapon

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Callin order: gadget:UnitPreDamaged and script.HitByWeapon

Post 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?
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

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

Post 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.
User avatar
CarRepairer
Cursed Zero-K Developer
Posts: 3359
Joined: 07 Nov 2007, 21:48

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

Post 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.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

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

Post 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
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

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

Post 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.
Last edited by Silentwings on 01 Sep 2013, 18:11, edited 1 time in total.
User avatar
CarRepairer
Cursed Zero-K Developer
Posts: 3359
Joined: 07 Nov 2007, 21:48

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

Post 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.
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

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

Post by Silentwings »

Because I've no objection to updating my code if it helps make the api more sensible.
User avatar
CarRepairer
Cursed Zero-K Developer
Posts: 3359
Joined: 07 Nov 2007, 21:48

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

Post 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)"
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

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

Post 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.
User avatar
CarRepairer
Cursed Zero-K Developer
Posts: 3359
Joined: 07 Nov 2007, 21:48

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

Post 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.
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

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

Post by Silentwings »

Knorke's post is a day old ;)
silentwings wrote:... it happened in https://github.com/spring/spring/commit ... 275ad8f65d
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

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

Post by knorke »

my thread brought a change :shock:
thanks obama.
Post Reply

Return to “Lua Scripts”