How to get the nearest 3 units?

FireBladesX

Eating my wings!
Reaction score
123
How do you pick the closest X units to the caster? Like the nearest 3?
In other words, with a trigger, I'd like to be able to get the three closest units to a point, and do something to them.

Relatively resolved.
 
Sure.
With a trigger, I'd like to be able to get the three closest units to a point, and do something to them.

I'll edit the first post, why not.
 
I think there were a JASS system to do that.

I think it was something like loop units whtin 1 range + var
where var increases and then store thoose units etc.
 
It's not that hard. You make 3 variables for the 3 closest units. Then you pick every unit on the map beside the caster and put it into a unit group variable.
Then pick a random unit and put it on the first variable. Remove it from the unit group.
Do the same with the other 2 variables so you have a unit in each of them and the units aren't in the group variable anymore.
Count the units in the group and make a loop that loops as often as the number of units you just counted.

In the loop you do the following:
Pick a random unit. Compare the distance of the unit to the caster with the distance of the unit in the first unit variable to the caster. If the distance is smaller, then you overwrite the first unit variable with the randomly picked unit and remove the randomly picked unit from the unit group variable.
If the distance is bigger you do the same thing for the second and the third unit variable. If all units in the 3 variables are closer to the caster then the randomly picked one you remove the randomly picked one from the unit group.

Now you have the 3 closest units in the 3 variables.

Hope you understand it :<
 
I remember doing a spell like that once. wait a minute or two iand i will see if i can locate it.
 
Yes, I do understand. I think I tried that in an old map and it failed, but I was bad at WE back then, heh.
Thx every1 :D
I'll look at what you have too, grym.
Editing my first post.


I just thought of a roundabout way... Using damage detection and a fan of knives. Less work, but I won't use it.
 
okay here it is. the spell is called Chronic Pain and it damages the 3 closest enemies every 5 sec.

i'm sorry about the file size it contains an uncompressed skin in it.
 

Attachments

  • PhysiqueMage.w3x
    422.6 KB · Views: 80
Sorting... for the times when you want 2 or 3 or 5 or ...

Code:
Set Count = 3
Set Point = Position of (Triggering unit)
Set UnitGroup = Unit within 500 of Point matching (((Matching unit) belongs to an enemy of (Owner of (Triggering unit))) equal to true
Set TempInteger = 0
Pick every unit in UnitGroup and do
    Set TempInteger = TempInteger + 1
    Set Units[TempInteger] = (Picked unit)
    Set TempPoint = Position of (Picked unit)
    Set Distances[TempInteger] = Distance from Point to TempPoint
    Custom script: call RemoveLocation( udg_TempPoint )
Custom script: call RemoveLocation( udg_Point )
Custom script: call DestroyGroup( udg_UnitGroup )
For each Integer A from 1 to (TempInteger - 1)
    For each Integer B from (Integer A + 1) to TempInteger
        If Distances[Integer A] greater than Distances[Integer B] then
            Set TempReal = Distances[Integer A]
            Set Distances[Integer A] = Distances[Integer B]
            Set Distances[Integer B] = TempReal
            Set TempUnit = Units[Integer A]
            Set Units[Integer A] = Units[Integer B]
            Set Units[Integer B] = TempUnit
For each Integer A from 1 to Count
    Game - Display: "Nearest " + String(Integer A) + ": " + Name of (Units[Integer A])
 
have you got JassNewGen ?
if yes you could use following JassScript ...
(i could edit it aswell so you could use it even without JassNewGen)
JASS:
library NearestUnits
globals
    private player TempPlayer
endglobals

private function IsEnemy takes nothing returns boolean
return IsPlayerEnemy(TempPlayer,GetOwningPlayer(GetFilterUnit()))and GetWidgetLife(GetFilterUnit())&gt;0
endfunction

function getNearestUnits takes unit u, integer units, integer maxradius returns group
local location l=GetUnitLoc(u)
local group g=CreateGroup()
local integer radius=0
loop
    set TempPlayer=GetOwningPlayer(u)
    call GroupEnumUnitsInRangeOfLoc(g, l, radius, Condition(function IsEnemy))
    if(CountUnitsInGroup(g)==units or radius&gt;maxradius)then
        exitwhen(true)
    endif
    call GroupClear(g)
    set radius=radius+10
endloop
set l=null
return g
endfunction
endlibrary

You call it like following:

Custom Script - set udg_<SomeGroupVariable>=getNearestUnits(udg_<SomeUnitVariable>,<integer units>,<integer maxradius>)
SomeUnitVariable is the central unit you search around for units near it...
the integer units is the ammount of units you want ...
the integer maxradius is the maximum radius you want to search in..
so
set udg_TempGroup=getNearestUnits(udg_TempUnit,3,1000)
will get 3 nearest enemy living units around udg_TempUnit in an radius of 1000,
or 0,1,2 units if there arent 3 units in the radius of 1000.

after you used the units to do your stuff dont forget to clean the unitgroupvariable with sth like
Custom script: call DestroyGroup(udg_TempGroup)

its pretty easy to use:
Trigger:
  • Test
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • Set TempUnit = Blademaster 0002 &lt;gen&gt;
      • Custom script: set udg_TempGroup=getNearestUnits(udg_TempUnit,3,1000)
      • Unit Group - Pick every unit in TempGroup and do (Actions)
        • Loop - Actions
          • Game - Display to (All players) the text: (Name of (Picked unit))
          • Unit - Explode (Picked unit)
      • Custom script: call DestroyGroup(udg_TempGroup)
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    News portal has been retired. Main page of site goes to Headline News forum now
  • The Helper The Helper:
    I am working on getting access to the old news portal under a different URL for those that would rather use that for news before we get a different news view.
  • Ghan Ghan:
    Easily done
    +1
  • The Helper The Helper:
    https://www.thehelper.net/pages/news/ is a link to the old news portal - i will integrate it into the interface somewhere when i figure it out
  • Ghan Ghan:
    Need to try something
  • Ghan Ghan:
    Hopefully this won't cause problems.
  • Ghan Ghan:
    Hmm
  • Ghan Ghan:
    I have converted the Headline News forum to an Article type forum. It will now show the top 20 threads with more detail of each thread.
  • Ghan Ghan:
    See how we like that.
  • The Helper The Helper:
    I do not see a way to go past the 1st page of posts on the forum though
  • The Helper The Helper:
    It is OK though for the main page to open up on the forum in the view it was before. As long as the portal has its own URL so it can be viewed that way I do want to try it as a regular forum view for a while
  • Ghan Ghan:
    Yeah I'm not sure what the deal is with the pagination.
  • Ghan Ghan:
    It SHOULD be there so I think it might just be an artifact of having an older style.
  • Ghan Ghan:
    I switched it to a "Standard" article forum. This will show the thread list like normal, but the threads themselves will have the first post set up above the rest of the "comments"
  • The Helper The Helper:
    I don't really get that article forum but I think it is because I have never really seen it used on a multi post thread
  • Ghan Ghan:
    RpNation makes more use of it right now as an example: https://www.rpnation.com/news/
  • The Helper The Helper:
  • The Helper The Helper:
    What do you think Tom?
  • tom_mai78101 tom_mai78101:
    I will have to get used to this.
  • tom_mai78101 tom_mai78101:
    The latest news feed looks good
  • The Helper The Helper:
    I would like to see it again like Ghan had it the first time with pagination though - without the pagination that view will not work but with pagination it just might...
  • The Helper The Helper:
    This drink recipe I have had more than a few times back in the day! Mind Eraser https://www.thehelper.net/threads/cocktail-mind-eraser.194720/

      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