Page 1 of 1
shipTakingDamage event handler question
Posted: Thu Apr 19, 2012 9:24 am
by Smivs
I'm trying to trigger an event when the player ship has no shields and is taking damage, so the 'shipTakingDamage' handler looked ideal.
Wiki wrote:The shipTakingDamage handler is called when a ship sustains damage. (For player ships this only happens after the shields are down)
However this is not working as expected. The event is being triggered as soon as the player ship takes a hit. Just one hit from a pulse laser is triggering the event, even when the shields show no loss whatsoever. This is what I'd expect from the 'shipBeingAttacked' handler, but not the 'shipTakingDamage' handler.
I have tested this with a base Cobra3 (no shield boosters etc) up to my iron-ass Boa Clipper and the effect is the same.
Is this right?
Re: shipTakingDamage event handler question
Posted: Thu Apr 19, 2012 9:31 am
by Kaks
Sounds like a bug! Will look into it & report asap!
Re: shipTakingDamage event handler question
Posted: Thu Apr 19, 2012 9:36 am
by Commander McLane
It does not sound right, especially as the description explicitly says otherwise. (I also notice that the event isn't listed at all on the world script event handler page, only for ship scripts.)
You could of course (sort of) work around it by using an additional check:
Code: Select all
if(player.ship.forwardShield > 0 && player.ship.aftShield > 0) return;
That's however no real workaround, because of the two different shields. After all, your ship can take damage if only one of the shields is depleted. For a proper check you'd have to keep track of both shield values, and return if one of them decreased (meaning that the shield took the damage).
Thinking about it, there is also the issue of subentities. It may be that it was one of your subentities that took the damage instead of your shields. I have no idea how the event handler deals with subentities.
Re: shipTakingDamage event handler question
Posted: Thu Apr 19, 2012 9:46 am
by another_commander
I think it works as expected. The shipTakingDamage handler takes the damage amount as its first argument and when this is 0, then it means that a shield rather than hull was hit. See the log entries below from the player ship taking damage on shield, followed by damage on the hull.
11:39:59.322 [jstest.shipTakingDamage]: Player ship taking 0 damage by [Ship "GalCop Viper" position: (-47667.8, 57979.1, 428768) scanClass: CLASS_POLICE status: STATUS_IN_FLIGHT] by means of energy damage
11:39:59.572 [jstest.shipTakingDamage]: Player ship taking 6.374378204345703 damage by [Ship "GalCop Viper" position: (-47672.4, 57957.9, 428774) scanClass: CLASS_POLICE status: STATUS_IN_FLIGHT] by means of energy damage
I can't put my hand on the fire for this, but if I recall correctly when the handler was first committed, it was like this by design.
Re: shipTakingDamage event handler question
Posted: Thu Apr 19, 2012 10:12 am
by Commander McLane
another_commander wrote:I think it works as expected. The shipTakingDamage handler takes the damage amount as its first argument and when this is 0, then it means that a shield rather than hull was hit. See the log entries below from the player ship taking damage on shield, followed by damage on the hull.
That makes it even easier to deal with the case (I should've read the documentation properly before posting
):
Code: Select all
this.shipTakingDamage = function(amount, whom, type)
{
if(amount === 0) return;
}
Re: shipTakingDamage event handler question
Posted: Thu Apr 19, 2012 10:16 am
by Eric Walch
Smivs wrote:I'm trying to trigger an event when the player ship has no shields and is taking damage, so the 'shipTakingDamage' handler looked ideal.
Wiki wrote:The shipTakingDamage handler is called when a ship sustains damage. (For player ships this only happens after the shields are down)
However this is not working as expected. The event is being triggered as soon as the player ship takes a hit.
I think another_commander is right. The handler is working as planned, only the description in the wiki is wrong. The handler fires when hit and than shows the amount of damage. That way a script is able to transfer energy from the banks to the shields on a hit or do other interesting shield alterations.
Re: shipTakingDamage event handler question
Posted: Thu Apr 19, 2012 10:39 am
by Smivs
Thanks all. Yes, it seems the wording in the Wiki is misleading/confusing. By 'down' I assumed it meant 'down to zero', whereas it actually means 'down below maximum'. Perhaps it should read
For player ships this happens if the shields are below maximum
or similar.
No worries. By following McLanes suggestion of doing a shield check as well, I've pretty much got things working as I wanted.
Re: shipTakingDamage event handler question
Posted: Thu Apr 19, 2012 11:07 am
by Commander McLane
Smivs wrote:By following McLanes suggestion of doing a shield check as well, I've pretty much got things working as I wanted.
The second suggestion of checking for '0' as amount is much better.
Re: shipTakingDamage event handler question
Posted: Thu Apr 19, 2012 11:36 am
by Smivs
Commander McLane wrote:Smivs wrote:By following McLanes suggestion of doing a shield check as well, I've pretty much got things working as I wanted.
The second suggestion of checking for '0' as amount is much better.
Yes, both methods seem to work identically, but the check for amount is certainly more elegant. Thank you.
Re: shipTakingDamage event handler question
Posted: Thu Apr 19, 2012 11:48 am
by Kaks
OK, it would be simple to change, but it looks like the 1.76 behaviour allows for more flexibility. Updating the wiki it is!
Re: shipTakingDamage event handler question
Posted: Thu Apr 19, 2012 12:04 pm
by Commander McLane
Smivs wrote:Commander McLane wrote:Smivs wrote:By following McLanes suggestion of doing a shield check as well, I've pretty much got things working as I wanted.
The second suggestion of checking for '0' as amount is much better.
Yes, both methods seem to work identically, but the check for amount is certainly more elegant. Thank you.
It's not only more elegant, but also the only variant which gives correct results. The two approaches will stop working identically as soon as either your front or your rear shield is depleted. From then on the first approach is bound to give false results.
Re: shipTakingDamage event handler question
Posted: Thu Apr 19, 2012 4:55 pm
by Thargoid
Yes, A_C's recollection is correct. I recall the discussion I had with Ahruman on the subject when it was set up, as the event was my request so I could write IronHide properly.