Difference between revisions of "OnHit - ObjectReference"

2,357 bytes removed ,  18:46, 3 March 2021
→‎Examples: removed content that contradicts the rest of this article and the rest of the wiki; see talk page
imported>DavidJCobb
(→‎Notes: removed a note that makes zero sense)
imported>DavidJCobb
(→‎Examples: removed content that contradicts the rest of this article and the rest of the wiki; see talk page)
Line 28: Line 28:
EndEvent
EndEvent
</source>
</source>
To avoid the hits from enchantments running the code block one must compare the ''akSource'' to the actual reference that hit the actor/object.
For instance, say we have a weapon with 2 enchantments called "Fire/Ice Sword." When hit with this sword the hit registers as 3 separate hits, but you have code such as:
<source lang="papyrus">
Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, \
  bool abBashAttack, bool abHitBlocked)
if(akSource as Weapon)
  Variable1 -= 100
EndEvent
</source>
This would actually subtract 300 from Variable1 since the event was called 3 times simultaneously, even though the akSource has been cast as a weapon papyrus counts the hit from the enchantments as "weapons" too so the enchantments are reference as (akSource as Weapon) as well. So the solution is to be more explicit.
<source lang="papyrus">
Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, \
  bool abBashAttack, bool abHitBlocked)
Form RightHandSword = akAggressor.GetEquippedWeapon(0); this gets the sword that the enemy had in their right hand and stores it as an object variable
if((akSource as weapon) == RightHandSword
;do code stuff
EndEvent
</source>
This will still call the event 3 times, but the code will only fire once when (akSource as weapon) == RightHandSword
*1st hit: Sword: (akSource as weapon) == RightHandSword - fires code
*2nd hit: Enchant1: (akSource as weapon) == SomeEnchant1 - nothing happens
*3rd hit: Enchant2: (akSource as weapon) == SomeEnchant2 - nothing happens
If you had the exact same script but only want the code to run when the actor/object that has the script with the OnHit Event is hit with a certain enchantment, regardless of the sword then:
<source lang="papyrus">
Enchantment Property ''TheEnchantment'' Auto
Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, \
  bool abBashAttack, bool abHitBlocked)
if((akSource as weapon) == ''TheEnchantment''
;do code stuff
EndEvent
</source>
With this code the Actor/Object with the OnHitEvent script could be hit by 5 different weapons but the onhit event will only fire when the weapon has ''TheEnchantment'' and it will only fire once.


== Notes ==
== Notes ==
Anonymous user