• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Detection if the spell killed an unit

Status
Not open for further replies.
Level 2
Joined
Feb 19, 2023
Messages
4
Is there a way to detect if a spell killed an unit? I want to make a spell that If spell's lightning kills the unit - summon an unit
 
Level 25
Joined
Sep 26, 2009
Messages
2,390
As far as I know: no, there is not a way to detect if spell killed a unit - when unit dies, the information about damage is not available.

You can, however, detect when unit takes damage - that happens before the damage is applied. At that point you can detect attack type and damage type.
The drawback is that attack and damage type comparisons are not available in GUI. You would need to use a little bit of JASS to get that. Also, you would need to correctly calculate if the damage unit is about to take will actually kill it (for example, you need to consider if unit has Runed Bracers ability, which reduces spell damage taken, etc.)

All of the above can be avoided if you use some damage detection system like Bribe's (Damage Engine 5.9.0.0) which already gives you a nice access to the attack and damage type in GUI as well as event for detecting lethal damage dealt to unit.

But even then, you do not know for certain which spell killed the unit: if you want to summon a unit when another unit has been killed by a "specific" spell with lightning damage, then you will have a hard time.
Consider the following situation:
  • Let's say you have a hero with 2 lightning abilities - one is a debuff that deals periodic lightning damage, the second spell is like chain lightning- it deals lightning damage immediately
  • You want to summon a unit if any unit has been killed by the chain lightning spell
  • Your hero casts the lightning debuff on a unit and then casts chain lightning on the unit, killing it
  • Just based on the attack and damage type taken, how will you know which spell killed the unit? Was it the debuff or the chain lightning? Both have same attack and damage type
You can avoid these situations by making sure that the [attack type] and [damage type] of the spell is unique for the hero - that way you know for sure which spell killed it. Another option is to make the damage difference between the spells big enough that you just know if it was one spell or the other.

Edit: there may be also some other (more complicated) solutions to the dilemma, like caching the information that hero just casted that spell on that specific target. But if that is a viable solution depends on how the spell that summons unit actually works.
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,596
A nice solution with the Damage Engine is to have two abilities, a fake one used by your Hero that's simply meant to run the spell trigger, and a real one used by a Dummy unit with the actual damaging effects. Then in your Kill Trigger you can check if the (Killing unit) has the "real" ability. If it does, we know for a fact that the source of the lethal damage was from that ability.

The problem here is that the Event Response (Killing unit) as well as the (Damage source) is going to point to the Dummy unit and not the Hero. This can mess with your triggers that rely on these Event Responses. But by using a Unit Indexer or a Hashtable you can easily link the Dummy unit and the Hero together, which allows for you to then get the Hero from the Dummy unit.

Here's an example using a Unit Indexer.

Upon creating our Dummy unit we immediately link it to our Hero (the unit that cast the spell):
  • Events
    • Unit - A unit Starts the effect of an ability
  • Conditions
    • (Ability being cast) Equal to Fireblast (Hero)
  • Actions
    • Unit - Create 1 Dummy unit...
    • Set Variable DummyToHero[Custom value of (Last created unit)] = (Casting unit)
    • Unit - Add Fireblast (Dummy) to (Last created unit)
    • Unit - Order (Last created unit) to cast it's spell...
Then have another trigger which detects when the Dummy kills a unit and give the kill credit to the Hero instead:
  • Events
    • Unit - A unit dies
  • Conditions
    • (Level of Fireblast (Dummy) for (Killing unit)) Greater than 0
  • Actions
    • Set Variable Killer = DummyToHero[Custom value of (Killing unit)]
    • Unit - Set life percentage of Killer to 100.00%
Notes:
  • The (Casting unit) doesn't need to be a Hero.
  • The Dummy unit in the Object Editor shouldn't have any abilities besides Locust.
  • The Dummy unit should be based on the Locust unit with the following IMPORTANT changes: Movement Type = None, Speed Base = 0, Attacks Enabled = None, Model = None, Shadow = None, Abilities = Locust.
  • The Dummy unit needs a long enough expiration timer that ensures it doesn't get removed before killing something.
  • If you were already using the Custom Value action in your triggers then you'll need to change that to take advantage of the Unit Indexer instead.
  • Make sure you only have one Unit Indexer in your map at a time.
 
Last edited:
Status
Not open for further replies.
Top