Best way to order an ai unit to cast an ability

Level 8
Joined
Dec 31, 2016
Messages
300
What is the best (logic) way to order a unit to cast it'S ability? I mean the condition for it. For now I just have them cast it whenever they attack. But is there a better way? Can I put more logic into it?
 
Level 6
Joined
Nov 4, 2019
Messages
120
It depends on the case and the type of ability, but my personal favorite method is with periodic timers that check for potential targets in a radius around the unit and make it cast the spell if there's a unit that meets the requirements. You can do a check every time it attacks as well, but if it's a unit targeting spell then the unit will lock up in most cases, since the attack event happens at the same time you order it to target a unit. Instant cast spells work fine if you overwrite the attack with a new order.

Below is a trigger I use to make priests cast Inner Fire on friendly units (I deleted a few things so it's easier to understand in this context). The trigger picks a random priest, filters units in an area around it, and orders the priest to cast it if there are any valid targets. It also checks for stronger units to target first.

  • Priest Inner Fire
    • Events
      • Time - Every 0.55 seconds of game time
    • Conditions
      • (Number of units in Priests) Greater than 0
    • Actions
      • Set VariableSet Unit = (Random unit from Priests)
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Current order of Unit) Not equal to (Order(dispel))
        • Then - Actions
          • Set VariableSet Point = (Position of Unit)
          • Set VariableSet UnitGroup = (Units within 500.00 of Point matching ((((Matching unit) belongs to an enemy of (Owner of Unit).) Equal to False) and ((((Matching unit) has buff Inner Fire) Equal to False) and ((Level of (Matching unit)) Greater than 3))).)
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Number of units in UnitGroup) Greater than 0
            • Then - Actions
              • Set VariableSet Unit2 = (Random unit from UnitGroup)
              • Unit - Order Unit to Human Priest - Inner Fire Unit2
            • Else - Actions
              • Set VariableSet UnitGroup2 = (Units within 500.00 of Point matching ((((Matching unit) belongs to an enemy of (Owner of Unit).) Equal to False) and (((Matching unit) has buff Inner Fire) Equal to False)).)
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • (Number of units in UnitGroup2) Greater than 0
                • Then - Actions
                  • Set VariableSet Unit2 = (Random unit from UnitGroup2)
                  • Unit - Order Unit to Human Priest - Inner Fire Unit2
                • Else - Actions
              • Custom script: DestroyGroup(udg_UnitGroup2)
          • Custom script: RemoveLocation(udg_Point)
          • Custom script: DestroyGroup(udg_UnitGroup)
        • Else - Actions
 
Level 32
Joined
Jan 30, 2013
Messages
11,414
isn't there a way to tap into the built in AI that does this pretty well? I think I saw a tutorial on that somewhere...
 
Top