• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Loops and arrays

Status
Not open for further replies.
Level 2
Joined
Mar 4, 2010
Messages
9
Basically, I have an array of 10 regions, and I want to check whether a unit is in any of them.

Code:
function RegionCheck takes location l returns boolean

    local integer i = 0
    local boolean b = false
    local real x = GetLocationX(l)
    local real y = GetLocationY(l)

    loop
    exitwhen i == 11 or b == true
    if(IsPointInRegion(udg_Regions[i], x, y) == true) then
    set b = true
    endif
    set i = i+1
    endloop

    return b

endfunction

However, this isn't working, and I'm wondering what the problem is...

The region is created fine, and the function will work fine if you give the regions individually (e.g. udg_Regions[1]), so I'm thinking this might be a limitation of the loop function...

Is there any way in which I can get this to work, or approximate something resembling it?
 
Level 2
Joined
Mar 4, 2010
Messages
9
Hmmmm, it's still not working properly...

I tried this:

Code:
function MineFieldCheck takes location l returns integer

    local integer i = 0
    local boolean b = false
    local location lo = l
    local real x = GetLocationX(lo)
    local real y = GetLocationY(lo)

    call DisplayTextToForce( GetPlayersAll(), R2S(x) )
    call DisplayTextToForce( GetPlayersAll(), R2S(y) )

    loop
    exitwhen i == 10 or b == 1 
    if(IsPointInRegion(udg_MineFields[i], x, y) == true) then
    set b = true
    endif
    set i = i+1
    endloop

    return b

endfunction

And the x and y values seem to fit the unit's location, so I don't think that the problem is the location, but I could be wrong...
 
Level 2
Joined
Mar 4, 2010
Messages
9
Hmmm, that's still not working...

Code:
function MineFieldCheck takes unit u returns boolean

    local integer i = 0
    local boolean b = true
    local real x = GetUnitX( u )
    local real y = GetUnitY( u )

    call DisplayTextToForce( GetPlayersAll(), R2S(x) )
    call DisplayTextToForce( GetPlayersAll(), R2S(y) )

    loop
    exitwhen i == 3 or b == true
    if(IsPointInRegion(udg_MineFields[i], x, y) == true) then
    set b = true
    endif
    set i = i+1
    endloop

    return b

endfunction

Just to provide a little bit more background on this function: It's being used as a condition and gets passed a unit to run through the checks on.

Also, as far as I can tell, the x and y values are correct and accurate, and the problem has to be something to do with the looping feature...
 
You could always do this:
JASS:
function IsUnitInARegion takes unit u returns boolean
    local integer i = 0
    loop
        exitwhen i == 11
        if IsUnitInRegion(udg_Regions[i],u) then
            return true
        endif
        set i = i + 1
    endloop
    return false
endfunction

Instead of the other function.

EDIT: In the last one you posted, you set b to "true" initially. =P
 
Level 2
Joined
Mar 4, 2010
Messages
9
About the b = true, all I can say is opps. :eek:

I tried that function you posted above, it still doesn't work. I think there must be some weird limit here involving regions and loops or something...
 
Level 2
Joined
Mar 4, 2010
Messages
9
I've run that script, and the region definitely exists (Or at least doesn't through up a debug message). This is very strange... :confused:
 
Level 2
Joined
Mar 4, 2010
Messages
9
Oh, yeah, I probably should have put that in at the beginning. Sorry...


Basically, I'm using it to approximate a minefield system, in a map where everything moves once in turns, like chess. This is going into the movement trigger, that picks each unit and moves it around every turn. What I do is use it as the condition in a if/then construction thingy:

Code:
if IsUnitInARegion(GetEnumUnit()) then
    call <stuff...>
    endif

That's all I do with the boolean, but it's definitely the condition of the If clause, because I've put debug messages in the if, and it's definitely getting a false result back from the function.
 
Level 4
Joined
May 17, 2005
Messages
94
Basically, I have an array of 10 regions, and I want to check whether a unit is in any of them.
Considering you're looping from 0 through 10, I believe that would be 11 regions.

The location l has to be declared within this very function, you know that, right?
No. Variables passed to a function do not need to be declared within that function. That makes no sense, and is certainly not how anyone codes anything. If it were, many of our functions would be like twice as long because of rampant local variable declaration.



Anyway, I have no clue what's wrong. I would imagine it's a problem in either how you're passing things to the region check function, or it's an issue with managing the returned boolean, because I can conclusively say that the function itself works:
 

Attachments

  • RegionFunctionality.w3x
    15.2 KB · Views: 45
Status
Not open for further replies.
Top