How to make workers attack on rally point

Level 9
Joined
Sep 25, 2013
Messages
456
Hi! Im working on adding additional resources to my game such as food and stone. Everything is working fine so far. I made food in the form of pigs which you slaughter with an ability they have. This replaces them with a neutral hostile slaughtered pig unit that only workers can attack. You gain a certain amount of food every time a worker hits the slaughtered pig.

The problem i have is that when i set a town center's rally point to a slaughtered pig, the workers that are trained move to the rally point but don't attack the slaughtered pig unless i manually command them to. I want to make it so if the rally point is set to a slaughtered pig or a stone ore pile, the trained workers will automatically go and attack them.

How would i go about doing this. I couldn't find a way to have a trigger that checks what the rally point is set to. If i could i would make so if the rally point is set to a slaughtered pig then trained workers would attack it. I thought maybe when units are trained the rally point gives them a command, so i made a trigger that triggers when workers are commanded to move to a slaughtered pig, they are re-ordered to attack it.

Code:
Untitled Trigger 002
    Events
        Unit - A unit Is issued an order targeting an object
    Conditions
        ((Ordered unit) has buff Worker (All)) Equal to True
        Or - Any (Conditions) are true
            Conditions
                (Unit-type of (Target unit of issued order)) Equal to Slaughtered Pig (Nature)
                (Unit-type of (Target unit of issued order)) Equal to Gold Ore (Nature)
    Actions
        Unit - Order (Ordered unit) to Attack (Target unit of issued order)

But this trigger crashed the map. Does anyone have an idea of how to make this work? Thank you if you do!
 
Level 37
Joined
Mar 6, 2006
Messages
9,242
It most likely causes an infinite loop.

Event - A unit is issued an order targeting a point
Actions - Order unit to move to some place

The action triggers the event, which issues the order again, which triggers the event a gain...repeat and repet again and again.

The solution is

Event
-A unit is issued an order targeting a point
Actions
-Turn off this trigger
-Order unit to move to some place
-Turn on this trigger

Try to determine which trigger(s) you need to turn off.
 
Level 9
Joined
Sep 25, 2013
Messages
456
This is what i came up with. It works somewhat.

This trigger causes the trained worker to attack the resource that the rally point is set to. The resources are neutral passive units (gold ore or slaughtered pigs). I discovered that rally points that rally points only truly target your own units and neutral passive buildings. Rally points can at best only target the position of a neutral hostile unit or enemy unit, so making gold ore unit neutral hostile so workers would attack them automatically wouldn't work.

Also, rally points for whatever reason only target actual unit models, and not doodads or imported doodad models like the gold ore model i was using. Quite arbitrary and complex nonsense.

Code:
Worker Trained Resource
    Events
        Unit - A unit Finishes training a unit
    Conditions
        ((Trained unit) has buff Worker (All)) Equal to True
    Actions
        Unit - Order (Trained unit) to Attack (Rally-Point of (Triggering unit) as a unit)

The next step was to have workers check the unit type of the unit they are ordered to attack so they wont attack allied or friendly units.

Code:
Worker Attacks Resource
    Events
        Unit - A unit Is attacked
    Conditions
        ((Attacking unit) has buff Worker (All)) Equal to True
    Actions
        If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            If - Conditions
                Or - Any (Conditions) are true
                    Conditions
                        (Unit-type of (Attacked unit)) Equal to Gold Ore (Nature)
                        (Unit-type of (Attacked unit)) Equal to Slaughtered Pig (Nature)
            Then - Actions
            Else - Actions
                Unit - Order (Attacking unit) to Stop

After this I had to make sure that workers that are already on the map also attack resources when they are rick-click ordered to them.

Code:
Worker Targets Resource
    Events
        Unit - A unit Is issued an order targeting an object
    Conditions
        ((Ordered unit) has buff Worker (All)) Equal to True
        Or - Any (Conditions) are true
            Conditions
                (Unit-type of (Target unit of issued order)) Equal to Gold Ore (Nature)
                (Unit-type of (Target unit of issued order)) Equal to Slaughtered Pig (Nature)
    Actions
        Trigger - Turn off (This trigger)
        Unit - Order (Ordered unit) to Attack (Target unit of issued order)
        Trigger - Turn on (This trigger)

The last thing i had to do was make sure that the workers move on to another resource pile once the one they are attacking dies.

Code:
Slaughtered Pig Continue
    Events
        Unit - A unit Dies
    Conditions
    Actions
        If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            If - Conditions
                (Unit-type of (Triggering unit)) Equal to Slaughtered Pig (Nature)
            Then - Actions
                Set tempUnitGroup = (Units within 300.00 of (Position of (Triggering unit)) matching (((Matching unit) has buff Worker (All)) Equal to True))
                Unit Group - Pick every unit in tempUnitGroup and do (Actions)
                    Loop - Actions
                        Set tempUnitGroup2 = (Units within 300.00 of (Position of (Picked unit)) matching ((Unit-type of (Matching unit)) Equal to Slaughtered Pig (Nature)))
                        Unit - Order (Picked unit) to Attack (Random unit from tempUnitGroup2)
                        Custom script:   call DestroyGroup (udg_tempUnitGroup2)
                Custom script:   call DestroyGroup (udg_tempUnitGroup)
                Unit - Remove (Triggering unit) from the game
            Else - Actions

This system works okay. The only problem now is with this last trigger directly above. For some reason workers don't always target a new slaughtered pig or gold ore pile even though i make sure there are plenty or them within range. This is all i need help with now. Please someone have an idea for this.
 
Top