• 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.

[Solved] IsUnitInRegion() ??

Status
Not open for further replies.
Level 19
Joined
Mar 18, 2012
Messages
1,716
In a quick way I can only image two solutions
  1. UnitInregion()
  2. You can write that function by yourself
    --> IsUnitInRect(unit, rect)
    -------------------------------------------------------------
    --> function IsUnitInRect takes unit WhichUnit , rect r returns boolean
    --> Now you GroupEnumUnit in that rect and run a FirstOfGroup loop
    --> loop
    ....
    --> if WhichUnit == u then
    --> return true
    --> endif
    --> endloop
    .....
    --> return false
    --> endfunction
    ------------------------------------------------------------
 
Last edited:
Level 11
Joined
Oct 11, 2012
Messages
711
In a quick way I can only image two solutions
  1. UnitInregion()
  2. You can write that function by yourself
    --> IsUnitInRect(unit, rect)
    -------------------------------------------------------------
    --> function IsUnitInRect takes unit WhichUnit , rect r returns boolean
    --> Now you GroupEnumUnit in that rect and run a FirstOfGroup loop
    --> loop
    ....
    --> if WhichUnit == u then
    --> return true
    --> endif
    --> endloop
    .....
    --> return false
    --> endfunction
    ------------------------------------------------------------

Thanks, BPower. :)
 
I would do something like this:
JASS:
function IsUnitInRect takes rect whichRect, unit whichUnit returns boolean
    local region r = CreateRegion()
    local boolean inside

    call RegionAddRect(r, whichRect)
    set inside = IsUnitInRegion(r, whichUnit)
    call RemoveRegion(r)
    set r = null

    return inside
endfunction

Or with a more vJASS-esque method:
JASS:
library IsUnitInRect
     
    globals
        private region r = CreateRegion()
        private boolean result
    endglobals

    function IsUnitInRect takes rect whichRect, unit whichUnit returns boolean
        call RegionAddRect(r, whichRect)
        set result = IsUnitInRegion(r, whichUnit)
        call RegionClearRect(r, whichRect)
        return result
    endfunction

endlibrary

Just because it has less complexity.
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
you have minx, miny, maxx, and maxy of the rect as well as x,y of the unit.

x >= minx and x <= maxx and y >= miny and y <= maxy

yay... common sense...

JASS:
 function IsUnitInRect takes rect r, unit u returns boolean
    return GetUnitX(u) < GetRectMaxX(r) and GetUnitY(u) < GetRectMaxY(r) and GetUnitX(u) > GetRectMinX(r) and GetUnitY(u) > GetRectMinY(r)
endfunction

... and its inline safe :D
 
Status
Not open for further replies.
Top