• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Code for when a unit attacks

Status
Not open for further replies.
Level 4
Joined
Dec 6, 2008
Messages
84
This one is tough, and I've been trying to make it flawlessly with only GUI for years now but it's still not perfect.

I need a trigger/JASS code that detects every time a unit attacks (that knows when a unit attacks, and not only the first attack, but all the ones that come after it as well). There needs to be no delay between when the unit starts its new attack and when the trigger records this attack. What makes this hard is that the attack is similar to that of a catapult: ranged, slow moving projectile that can be launched using the "attack ground" command.

This is how I went about doing it:

Unfortunately there is no event "when unit attacks". it works for when you target a unit or the ground but does not loop for when the unit attacks every other time after that. "unit takes damage" would work, but the attack does not necessarily deal damage, nor does it have a specific target (target ground).

I needed to take into account the fact that a unit might be ordered to attack before it can actually attack (out of range, etc.) I also needed to take into account the fact that there is a small delay depending on how much the unit has to turn to face the target before it can attack.

Using the distance from the target and the original facing degrees my code can find how many degrees the unit has to turn and add a delay in seconds using a linear function. If the unit is out of range I simply looped a check every 10 ms until it was in range, or until the order was changed. Now all I need is something that loops for every time the unit attacks.

Simply:

(Unit attacks)
Loop (until unit stops given attack order)
Wait (cooldown of unit)
(Unit attacks)

Every time the wait is finished, I know the unit attacks once again. Unfortunately this does not work for some reason. Not only is the cooldown value incredibly hard to convert into seconds using a function, for some reason the Wait (in-game seconds) command does not wait a fixed value: i get different wait times for the exact same cooldown o_O Does anyone know why this happens? I can explain in more detail and give an example if you need me to.

If anyone had the answer to this with JASS or even GUI that would be great. Also, if anyone knows how to convert unit cooldown values into seconds, that would help a lot!

If you have any suggestions or ideas feel free to post!
I've been trying to approach this with different methods:

Have small invisible neutral buildings with no collision and infinite regeneration in a grid all over the map, add an extra, invisible attack to the unit that has an infinite projectile speed, and use a simple trigger saying if (one of these) buildings is under attack (the buildings will have a defense only affected by this invisible attack) then I know the unit has just attacked once. But how do I add a second attack to a unit that stacks on top of its original attack?

I'm curious to see if anyone can figure this one out.

Many thanks,
Axel
 
Level 3
Joined
Jun 6, 2007
Messages
48
How about?

  • Unit Attacks
    • Events
      • Unit - A unit Is issued an order targeting an object
    • Conditions
      • (Issued order) Equal to (Order(attack))
      • (YOUR_UNIT) Equal to (Triggering Unit)
    • Actions
      • Wait 1.56 seconds (Chance to unit's cooldown as stated in Object Editor)
What do you plan to do after you know the unit attacks?
 
Level 4
Joined
Dec 6, 2008
Messages
84
That was the basic Idea I had in mind and it seemed simple enough excluding the turn rate and range issues. But the unit is a hero, and agility modifies cooldown, so within the game it is never static, which is why having a simple wait command doesn't work. Also, that needs to be in a a loop and has to take into account the fact that the hero can receive new attack orders which would overlap.

However, I managed to solve this just now with a cunning method.

Every time the unit attacks, I create a flying copy of the attacking unit (same cooldown, same agility, etc.) on top of the attacking unit with the same facing angle, and another flying unit at the center of the point on the ground being targeted.

I order the flying copy to attack the unit on the attack region, and have a trigger saying every time this unit is attacked, I know my hero has just started its new attack.

When I give my hero a new ground attack order, the attacked flying unit is killed and a new one is created at that new point. The copied unit is ordered to attack this new unit.

When I give my hero an order that isn't an attack, I kill the flying unit being attacked and order the copied unit to stop. If I give the hero a new attack order after this, I move the copied unit above his head with the same facing degrees, create a unit where the attack order is given, and so on.

If the attack is directed on unit and not the ground, the dummy flying unit will have to change position with respect to the targeted unit before the hero attacks again.

I haven't finished the code yet but this seems to work ^_^
 
Status
Not open for further replies.
Top