Get most Close unit?

Daskunk

SC2 Forum MVP - TheSkunk #386
Reaction score
186
I’m trying to make a unit attack the closest unit to him.

This is my trigger right now:
Code:
Me Chase
    Events
        Time - Every 0.50 seconds of game time
    Conditions
    Actions
        Set P1 = (Position of Oger 0009 <gen>)
        Set Target[2] = Target[1]
        Set TargetRange = 10000000
        Set G = (Units within 1000000.00 of P1 matching ((((Matching unit) is A structure) Equal to False) and (((Matching unit) is alive) Equal to True)))
        Unit Group - Pick every unit in G and do (Actions)
            Loop - Actions
                Set P2 = (Position of (Picked unit))
                If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    If - Conditions
                        (Integer((Distance between P1 and P2))) Less than TargetRange
                    Then - Actions
                        Set TargetRange = (Integer((Distance between P1 and P2)))
                        Set Target[1] = (Picked unit)
                        If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                            If - Conditions
                                Target[1] Not equal to Target[2]
                            Then - Actions
                                Unit - Order Oger 0009 <gen> to Attack Target[1]
                                Game - Display to (All players) the text: (The oger is chasing  + ((Name of (Owner of Target[1])) + !))
                            Else - Actions
                    Else - Actions
                Custom script:   call RemoveLocation (udg_P2)
        Custom script:   call RemoveLocation (udg_P1)
        Custom script:   call DestroyGroup (udg_G)
It isn’t working. It attempts to chase two different units at a time, even though they are different distances apart, and it seems to try and attack the target every time the trigger fires even though I though I attempted to fix that (Target[1] Not equal to Target[2]). Also, After it kills a unit sometimes it just sits there instead of finding another target.
 

UndeadDragon

Super Moderator
Reaction score
447
Because you have done this:

Code:
        Set G = (Units within 1000000.00 of P1 matching ((((Matching unit) is A structure) Equal to False) and (((Matching unit) is alive) Equal to True)))
        Unit Group - Pick every unit in G and do (Actions)
            Loop - Actions
                Set P2 = (Position of (Picked unit))

Followed by:

Code:
                        Set TargetRange = (Integer((Distance between P1 and P2)))

Would it not find the target range of all nearby units?
 

vypur85

Hibernate
Reaction score
803
Code:
Me Chase
    Events
        Time - Every 0.50 seconds of game time
    Conditions
    Actions
        Set P1 = (Position of Oger 0009 <gen>)
        Set Target[2] = Target[1]
        Set TargetRange = 10000000
        Set G = (Units within 1000000.00 of P1 matching ((((Matching unit) is A structure) Equal to False) and (((Matching unit) is alive) Equal to True)))
        Unit Group - Pick every unit in G and do (Actions)
            Loop - Actions
                Set P2 = (Position of (Picked unit))
                If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    If - Conditions
                        (Integer((Distance between P1 and P2))) Less than TargetRange
                    Then - Actions
                        Set TargetRange = (Integer((Distance between P1 and P2)))
                        Set Target[1] = (Picked unit)
[B]                        If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                            If - Conditions
                                Target[1] Not equal to Target[2]
                            Then - Actions
                                Unit - Order Oger 0009 <gen> to Attack Target[1]
                                Game - Display to (All players) the text: (The oger is chasing  + ((Name of (Owner of Target[1])) + !))
                            Else - Actions[/B]
                    Else - Actions
                Custom script:   call RemoveLocation (udg_P2)
        Custom script:   call RemoveLocation (udg_P1)
        Custom script:   call DestroyGroup (udg_G)

I think the bolded condition should be outside the loop. Not sure though. What you are doing now could be ordering the preplaced unit to attack the 'last picked unit in the loop'. (I think)

Edit:
Anyway, you could just pick units in playable map area instead of the 100000000.00 :p.
 

UndeadDragon

Super Moderator
Reaction score
447
I think thats right in the loop because out of loop...

Code:
        Set Target[2] = Target[1]
 

Daskunk

SC2 Forum MVP - TheSkunk #386
Reaction score
186
Because you have done this:

Code:
        Set G = (Units within 1000000.00 of P1 matching ((((Matching unit) is A structure) Equal to False) and (((Matching unit) is alive) Equal to True)))
        Unit Group - Pick every unit in G and do (Actions)
            Loop - Actions
                Set P2 = (Position of (Picked unit))

Followed by:

Code:
                        Set TargetRange = (Integer((Distance between P1 and P2)))

Would it not find the target range of all nearby units?
What do you mean? It sets target to be the closest one.


"Set Target[2] = Target[1]" makes it so that it stores the old target and checks checks to see if the new target is the same as the old one. If it is, it doesn't need to attack again.

@vypur85: Why would it need to be out of the loop? It might work that way as well, But I don't see much point too it. Ill try it. EDIT: No, Didn't work.
 

vypur85

Hibernate
Reaction score
803
It should be outside. Because I don't think you need to order the ogre to attack the picked unit in a loop (Meaning to attack loopingly. If there are 10 picked units detected, you will order it to attack 10 times.) Since you already define Target[1] in the loop, when outside the loop, the ogre should only attack that particular Target[1] unloopingly. Ahh... This is getting annoying to explain =.=". Not sure if I'm right though. I'm just using logic :p. Need to test the trigger myself to be able to really find out.

Anyway, use Real value instead of Integer... Should be more accurate and saves some time in conversion...
 

Daskunk

SC2 Forum MVP - TheSkunk #386
Reaction score
186
It should be outside. Because I don't think you need to order the ogre to attack the picked unit in a loop (Meaning to attack loopingly. If there are 10 picked units detected, you will order it to attack 10 times.) Since you already define Target[1] in the loop, when outside the loop, the ogre should only attack that particular Target[1] unloopingly. Ahh... This is getting annoying to explain =.=". Not sure if I'm right though. I'm just using logic :p. Need to test the trigger myself to be able to really find out.

Anyway, use Real value instead of Integer... Should be more accurate and saves some time in conversion...
Moving it out of the loop did not fix it. I tried it..
 

vypur85

Hibernate
Reaction score
803
Move that part outside of the loop and then remove the condition (Target[1] Not Equal to Target[2]). If it works, then it's the condition problem. In terms of outside or inside the loop, I'm quite sure it's supposed to be outside the loop (could work inside the loop too but then... as explained above). Try posting your current trigger. Outside the loop means at the bottom of the Unit Group loop.

Edit:
Oh yeah... Forgot something :p... You should also add a condition in the unit group matching part. Add 'Matching unit Not Equal to YourOgre'. Else, it can pick itself.
 

Daskunk

SC2 Forum MVP - TheSkunk #386
Reaction score
186
Code:
Me Chase
    Events
        Time - Every 0.10 seconds of game time
    Conditions
    Actions
        Set P1 = (Position of Oger 0009 <gen>)
        Set Target[2] = Target[1]
        Set TargetRange = 1000000000.00
        Set G = (Units within 1000000.00 of P1 matching ((((Matching unit) is A structure) Equal to False) and (((Matching unit) is alive) Equal to True)))
        Unit Group - Pick every unit in G and do (Actions)
            Loop - Actions
                Set P2 = (Position of (Picked unit))
                If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    If - Conditions
                        (Integer((Distance between P1 and P2))) Less than (Integer(TargetRange))
                    Then - Actions
                        Set TargetRange = (Distance between P1 and P2)
                        Set Target[1] = (Picked unit)
                    Else - Actions
                Custom script:   call RemoveLocation (udg_P2)
        If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            If - Conditions
                Target[1] Not equal to Target[2]
            Then - Actions
                Unit - Order Oger 0009 <gen> to Attack Target[1]
                Game - Display to (All players) the text: (The oger is chasing  + ((Name of (Owner of Target[1])) + !))
            Else - Actions
        Custom script:   call RemoveLocation (udg_P1)
        Custom script:   call DestroyGroup (udg_G)
Thats the current trigger. Ill remove the condition and see if it works. EDIT: No, it just makes it worse.
 

Daskunk

SC2 Forum MVP - TheSkunk #386
Reaction score
186
Was editing. Try the idea above.
Ok. That changed it for the better, but it still doesnt work all the way... Lemme try somthing.

EDIT: I had changed it so it was in the loop again. I took it out of the loop and it works! Thanks alot, +rep.
 

Daskunk

SC2 Forum MVP - TheSkunk #386
Reaction score
186
Trigger Help

This one:
Code:
Me Chase
    Events
        Time - Every 0.15 seconds of game time
    Conditions
    Actions
        Set P1 = (Position of Oger 0009 <gen>)
        If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            If - Conditions
                Target[2] Not equal to Target[1]
            Then - Actions
                Set Target[3] = Target[2]
            Else - Actions
        Set Target[2] = Target[1]
        Set TargetRange = 1000000000.00
        Set G = (Units within 1000000.00 of P1 matching ((((Matching unit) is A structure) Equal to False) and ((((Matching unit) is alive) Equal to True) and ((Matching unit) Not equal to Oger 0009 <gen>))))
        Unit Group - Pick every unit in G and do (Actions)
            Loop - Actions
                Set P2 = (Position of (Picked unit))
                If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    If - Conditions
                        (Integer((Distance between P1 and P2))) Less than (Integer(TargetRange))
                    Then - Actions
                        Set TargetRange = (Distance between P1 and P2)
                        Set Target[1] = (Picked unit)
                    Else - Actions
                Custom script:   call RemoveLocation (udg_P2)
        If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            If - Conditions
                Target[1] Not equal to Target[2]
            Then - Actions
                Unit - Order Oger 0009 <gen> to Attack Target[1]
                Sound - Play OgreWarcry1 <gen>
                Game - Display to (All players) the text: (The oger is chasing  + ((Name of (Owner of Target[1])) + !))
            Else - Actions
        Custom script:   call RemoveLocation (udg_P1)
        Custom script:   call DestroyGroup (udg_G)
^ Sets Target[1] To the nearest unit, And target[2] to be target one. If target 1 and 2 are the same, then the trigger doesnt do anything. If they are NOT the same, it runs the trigger and sets Target[3] to target[2].
Target[1] = Current target, current closest unit.
Target[2] = The closest unit the last time the trigger ran. Can be the same as 1.
Target[3] = The last target, but not the closest target of the last time the trigger ran, so it can't be the same as 1#. Its the last acuall target.

This trigger:
Code:
Me Smash
    Events
        Unit - A unit Is attacked
    Conditions
        (Attacking unit) Equal to Oger
    Actions
        Set PlayerScore = (Owner of Target[3])
This just sets PlayerScore (A player variable. Its the player that will get the score) to owner of target[3]. What its suppose to do is set playerscore to be a plain old player. Not a owner of a unit, becuase if the units dead, it shows nothing. Its suppose to be, anyways, allthough it is problably not doing it. This is probably whats wrong.

This one:
Code:
Me Kill
    Events
        Unit - A unit Dies
    Conditions
        (Killing unit) Equal to Oger
        (Unit-type of (Dying unit)) Equal to Paladin
    Actions
        Game - Display to (All players) the text: (The Oger has |cffff0000Pulverized|r  + ((Name of (Owner of (Dying unit))) + 's paladin!))
        If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            If - Conditions
                (Owner of (Dying unit)) Not equal to PlayerScore
            Then - Actions
                Game - Display to (All players) the text: ((Name of PlayerScore) +  was the last target! He gets credit for making the oger swich to targeting him.)
            Else - Actions
Should be pretty clear!

Thanks in advance.

The problem? If the last target is dead, it displays a blank string where the last player should be.
 

Atreyu

One Last Breath.
Reaction score
49
Code:
Target[2] Not equal to Target[1]

I think that's your problem.. Since that is an If/Then/Else condition. You put this:

Code:
Set Target[2] = Target[1]

As the Else - Actions.
 

Daskunk

SC2 Forum MVP - TheSkunk #386
Reaction score
186
Code:
Target[2] Not equal to Target[1]

I think that's your problem.. Since that is an If/Then/Else condition. You put this:

Code:
Set Target[2] = Target[1]

As the Else - Actions.
I checked over those to see if they were right or wrong one time And though I found it was right, but ill see. I was tired, and my own trigger where confusing me. :p

EDIT: Reread my triggers. Where is it in Else - Actions? I can't see any in the else actions. Did you just missread? They are in Then not else it looks like too me..
 

nabbig2

New Member
Reaction score
43
Sorry that this is too complicated for my knowledge, but I would just like to point out a typo:

Code:
(The [B]Oger [/B]has |cffff0000Pulverized|r  + ((Name of (Owner of (Dying unit))) + 's paladin!))
 

Daskunk

SC2 Forum MVP - TheSkunk #386
Reaction score
186
Sorry that this is too complicated for my knowledge, but I would just like to point out a typo:

Code:
(The [B]Oger [/B]has |cffff0000Pulverized|r  + ((Name of (Owner of (Dying unit))) + 's paladin!))
Yeah, I allready found that typo awhile ago. :)
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,494
> this is too complicated for my knowledge

You're not alone.

> Should be pretty clear!

Not really.
That thing is confusing as all hell.

What is that supposed to do?
What happens normally?
What exactly is "not normal"?
What happens instead of what you seem to expect?


> I took it out of the loop and it works!

Hm...
 

Daskunk

SC2 Forum MVP - TheSkunk #386
Reaction score
186
> Should be pretty clear!

Not really.
That thing is confusing as all hell.

What is that supposed to do?
What happens normally?
What exactly is "not normal"?
What happens instead of what you seem to expect?
Its supposed to display text giving credit to the target before the target that just got killed, when a unit is killed. However, It displays a blank string where the name of the last target should be, if the last target is dead.

> I took it out of the loop and it works!
Hm...
That was for a differnt problem entirely. That was getting the unit to target the correct unit, this is getting it to display the right text when a unit dies.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top