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

[vJASS] Regions and Rects

Status
Not open for further replies.
Level 4
Joined
May 23, 2010
Messages
83
Hey, I'm developing my entrance-exit-and-respawn system. I'm stuck in the entrance yet. Take a look at the code:
JASS:
library RegionsAndRespawn initializer onInit
    
    globals
        private rect array DUNGEON_GRAVEYARD_RECT 
        private rect array DUNGEON_ENTRANCE_RECT
        private rect array DUNGEON_ENTERED_RECT
        private rect array DUNGEON_RECT_RECT
        private rect array DUNGEON_EXIT_RECT
        
        private region array DUNGEON_GRAVEYARD
        private region array DUNGEON_ENTRANCE
        private region array DUNGEON_ENTERED
        private region array DUNGEON_RECT
        private region array DUNGEON_EXIT
        
        private integer DUNGEON_COUNT = 1
    endglobals
    
    private function DungeonEnter takes nothing returns nothing
        local unit u = GetEnteringUnit()
        local integer i = 0
        local real x
        local real y
        call BJDebugMsg("Unit entered region")
        loop
            if GetTriggeringRegion() == DUNGEON_ENTRANCE[i] then
                set x = GetRectCenterX(DUNGEON_ENTERED_RECT[i])
                set y = GetRectCenterY(DUNGEON_ENTERED_RECT[i])
                call BJDebugMsg("x: " + R2S(x) + "...... y: " + R2S(y))
                call SetUnitPosition(u, x, y)
            endif
            exitwhen i >= DUNGEON_COUNT - 1
            set i = i + 1
        endloop
        
        set u = null
    endfunction
    
    private function DungeonEnterCondition takes nothing returns boolean
        local unit u = GetEnteringUnit()
        return IsUnitType(u, UNIT_TYPE_HERO) and GetPlayerController(GetOwningPlayer(u)) == MAP_CONTROL_USER
    endfunction
    
    private function onInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        local integer i = 0
        
        //DUNGEONS SETTINGS
        set DUNGEON_RECT_RECT[0] = gg_rct_dung01
        set DUNGEON_GRAVEYARD_RECT[0] = gg_rct_dung01gy
        set DUNGEON_ENTRANCE_RECT[0] = gg_rct_dung01entrance
        set DUNGEON_ENTERED_RECT[0] = gg_rct_dung01entered
        set DUNGEON_EXIT_RECT[0] = gg_rct_dung01exit
        
        
        loop
            call RegionAddRect(DUNGEON_RECT[i], DUNGEON_RECT_RECT[i])
            call RegionAddRect(DUNGEON_GRAVEYARD[i], DUNGEON_GRAVEYARD_RECT[i])
            call RegionAddRect(DUNGEON_ENTRANCE[i], DUNGEON_ENTRANCE_RECT[i])
            call RegionAddRect(DUNGEON_ENTERED[i], DUNGEON_ENTERED_RECT[i])
            call RegionAddRect(DUNGEON_EXIT[i], DUNGEON_EXIT_RECT[i])
            call TriggerRegisterEnterRegion(t, DUNGEON_ENTRANCE[i], null)
            exitwhen i >= DUNGEON_COUNT - 1
            set i = i + 1
        endloop
        
        call TriggerAddAction(t, function DungeonEnter)
        call TriggerAddCondition(t, function DungeonEnterCondition)
        
        set t = null
    endfunction
    
endlibrary

The problem is: the region is not detected (I enter the region with my hero but it doesn't work - not even the message appears. Does anyone understands about regions here? I really need this system, it will make my life a lot better. Thanks in advance
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
JASS:
    private function DungeonEnterCondition takes nothing returns boolean
        local unit u = GetEnteringUnit()
        return IsUnitType(u, UNIT_TYPE_HERO) and GetPlayerController(GetOwningPlayer(u)) == MAP_CONTROL_USER
    endfunction

Just a suggestion, you should replace the value u here with GetEnteringUnit. You aren't able to null the pointer as it exists right now and as such you'll have memory leaks for units that are detected by your system.

JASS:
        set DUNGEON_RECT_RECT[0] = gg_rct_dung01
        set DUNGEON_GRAVEYARD_RECT[0] = gg_rct_dung01gy
        set DUNGEON_ENTRANCE_RECT[0] = gg_rct_dung01entrance
        set DUNGEON_ENTERED_RECT[0] = gg_rct_dung01entered
        set DUNGEON_EXIT_RECT[0] = gg_rct_dung01exit
        
        
        loop
            call RegionAddRect(DUNGEON_RECT[i], DUNGEON_RECT_RECT[i])
            call RegionAddRect(DUNGEON_GRAVEYARD[i], DUNGEON_GRAVEYARD_RECT[i])
            call RegionAddRect(DUNGEON_ENTRANCE[i], DUNGEON_ENTRANCE_RECT[i])
            call RegionAddRect(DUNGEON_ENTERED[i], DUNGEON_ENTERED_RECT[i])
            call RegionAddRect(DUNGEON_EXIT[i], DUNGEON_EXIT_RECT[i])
            call TriggerRegisterEnterRegion(t, DUNGEON_ENTRANCE[i], null)
            exitwhen i >= DUNGEON_COUNT - 1
            set i = i + 1
        endloop

I only see you initialize the values of DUNGEON_RECT_RECT[0] and such (for all the others too) but then in your loop you reference them as DUNGEON_RECT_RECT[i]. Are you sure this is what you're trying to do? I don't even see the necessity of the arrays, or the loop.

Also, as Maker mentioned, I don't see anywhere that you've done CreateRegion. If you aren't creating your region handles then nothing is going to be registered to the trigger (or even worse, you'll get bugs with the trigger).
 
Level 4
Joined
May 23, 2010
Messages
83
Berb, I just have one dungeon yet, the problem actually was the 'creation' of the regions. I'm making this system to simplify stuff when I make a new dungeon. Thanks for the improving and the nice tips!
 
Level 4
Joined
May 23, 2010
Messages
83
Then what would I do about this?
JASS:
                set x = GetRectCenterX(DUNGEON_ENTERED_RECT[i])
                set y = GetRectCenterY(DUNGEON_ENTERED_RECT[i])
                call SetUnitPosition(u, x, y)
 
Status
Not open for further replies.
Top