• 🏆 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!
  • ✅ The POLL for Hive's Texturing Contest #33 is OPEN! Vote for the TOP 3 SKINS! 🔗Click here to cast your vote!

Adding dynamic events based on data in hashtable

Level 3
Joined
Feb 25, 2013
Messages
16
Hey folks

This is related to a previous post of mine: here

I have a script that generates 4 Rects per player and saves them to a global hashtable (gg_tbl_playerRegions). This works fine

The problem occurs when I want to iterate this hashtable and add Trigger Events for the regions. It is as if the hashtable has not been populated before my Event-generation is run.

This DebugMsg just displays "Adding event: 0" all the time, but it should bet he ID of the region.
JASS:
call BJDebugMsg("Adding event: " + I2S(GetHandleId(toEnter)))

What am I doing wrong? (I'm open to hear about leak-problems, but that's not the issue at hand here I think :))



Code for generating regions:
JASS:
globals
    trigger gg_trg_addRegions
    hashtable gg_tbl_playerRegions = InitHashtable()
endglobals

function AdjustLocation takes location loc, integer i returns location
    local integer array multiX
    local integer array multiY
    set multiX[0] = 0
    set multiX[1] = 1
    set multiX[2] = 0
    set multiX[3] = -1

    set multiY[0] = 1
    set multiY[1] = 0
    set multiY[2] = -1
    set multiY[3] = 0

    return Location(GetLocationX(loc)+(udg_PlayerRgnOffset * multiX[i]),GetLocationY(loc)+(udg_PlayerRgnOffset * multiY[i]))
endfunction


function GenerateSpawnRegions takes nothing returns nothing
    local unit u = GetEnumUnit()   
    local rect array rects_Spawn
    local real rectRadius = 100.0
    local location uLoc = GetUnitLoc(u)

    local integer count = 0
    local location center
    local location min
    local location max
    loop
        exitwhen count > 3
        set center = AdjustLocation(uLoc, count)
        set min = Location(GetLocationX(center)-rectRadius,GetLocationY(center)-rectRadius)
        set max = Location(GetLocationX(center)+rectRadius,GetLocationY(center)+rectRadius)
        call SaveRectHandle(gg_tbl_playerRegions, GetHandleId(GetOwningPlayer(u)), count, RectFromLoc(min, max))   
        set count = count + 1
    endloop       
endfunction

function Trig_AddRegions_Actions takes nothing returns nothing   
    call ForGroupBJ( GetUnitsOfTypeIdAll(udg_CopterType), function GenerateSpawnRegions )
endfunction

//===========================================================================
function InitTrig_AddRegions takes nothing returns nothing
    set gg_trg_AddRegions = CreateTrigger(  )   
    call TriggerAddAction( gg_trg_AddRegions, function Trig_AddRegions_Actions )
endfunction

Code for generating Events:
JASS:
function Trig_UnitsEnterRegions_Actions takes nothing returns nothing
    //call BJDebugMsg("A unit entered the region!")
endfunction

function AddEventForRegionToTrigger takes nothing returns nothing
    local integer rectI = 0
    local integer rectMax = 3
    local rect toEnter
    loop
        exitwhen rectI > rectMax
        set toEnter = LoadRectHandle(gg_tbl_playerRegions, GetHandleId(GetEnumPlayer()), rectI )
        call BJDebugMsg("Adding event: " + I2S(GetHandleId(toEnter)))
        call TriggerRegisterEnterRectSimple( gg_trg_UnitsEnterRegions,  toEnter)
        set rectI = rectI + 1
    endloop
endfunction

//===========================================================================
function InitTrig_UnitsEnterRegions takes nothing returns nothing
    set gg_trg_UnitsEnterRegions = CreateTrigger(  )
        call BJDebugMsg("TRIGGER INITIALIZED")
    call ForForce( GetPlayersAll(), function AddEventForRegionToTrigger )
    call TriggerAddAction( gg_trg_UnitsEnterRegions, function Trig_UnitsEnterRegions_Actions )
endfunction
 
Level 41
Joined
Feb 27, 2007
Messages
5,222
UnitsEnterRegions’ init runs before AddRegions’ does, so no data is yet stored in the hashtable. I don’t see what would cause AddRegions to run anyway so I presume you’re executing it manually somewhere else.

Init functions and trigger declarations don’t have a defined order unless they are within libraries or scopes that properly require one another. It’s somewhat based on order the trigger was created, somewhat where it’s placed, and (I believe?) somewhat alphabetical.

Run the ForForce actions after the rects are created rather than in that InitTrig.
 
Level 3
Joined
Feb 25, 2013
Messages
16
UnitsEnterRegions’ init runs before AddRegions’ does, so no data is yet stored in the hashtable. I don’t see what would cause AddRegions to run anyway so I presume you’re executing it manually somewhere else.

Init functions and trigger declarations don’t have a defined order unless they are within libraries or scopes that properly require one another. It’s somewhat based on order the trigger was created, somewhat where it’s placed, and (I believe?) somewhat alphabetical.

Run the ForForce actions after the rects are created rather than in that InitTrig.
Moving the event logic to AddRegions did the trick. To clarify: AddRegions is set to Run on Map Initialization.

I already have a reference to the rect in the GenerateSpawnRegions, so it makes sense to add the event there. I also moved the CreateTrigger() to AddRegions.

Really appreciate the help! :thumbs_up:


Code for AddRegions
JASS:
globals
    trigger gg_trg_addRegions
    hashtable gg_tbl_playerRegions = InitHashtable()
endglobals

function AdjustLocation takes location loc, integer i returns location
    local integer array multiX
    local integer array multiY
    set multiX[0] = 0
    set multiX[1] = 1
    set multiX[2] = 0
    set multiX[3] = -1

    set multiY[0] = 1
    set multiY[1] = 0
    set multiY[2] = -1
    set multiY[3] = 0

    return Location(GetLocationX(loc)+(udg_PlayerRgnOffset * multiX[i]),GetLocationY(loc)+(udg_PlayerRgnOffset * multiY[i]))
endfunction

function GenerateSpawnRegions takes nothing returns nothing
    local unit u = GetEnumUnit()
    local player p = GetOwningPlayer(u)       
    local real rectRadius = 100.0
    local location uLoc = GetUnitLoc(u)

    local integer count = 0
    local location center
    local location min
    local location max
    local rect rectToSave
    loop
        exitwhen count > 3
        set center = AdjustLocation(uLoc, count)
        set min = Location(GetLocationX(center)-rectRadius,GetLocationY(center)-rectRadius)
        set max = Location(GetLocationX(center)+rectRadius,GetLocationY(center)+rectRadius)
        set rectToSave = RectFromLoc(min, max)
        call SaveRectHandle(gg_tbl_playerRegions, GetHandleId(p), count, rectToSave)
        call TriggerRegisterEnterRectSimple( gg_trg_UnitsEnterRegions,  rectToSave)   
        set count = count + 1
    endloop       
endfunction



function Trig_AddRegions_Actions takes nothing returns nothing   
    call ForGroupBJ( GetUnitsOfTypeIdAll(udg_CopterType), function GenerateSpawnRegions )
endfunction

//===========================================================================
function InitTrig_AddRegions takes nothing returns nothing
    set gg_trg_AddRegions = CreateTrigger(  )
    set gg_trg_UnitsEnterRegions = CreateTrigger(  )   
    call TriggerAddAction( gg_trg_AddRegions, function Trig_AddRegions_Actions )
endfunction

Code for UnitsEnterRegions
JASS:
function Trig_UnitsEnterRegions_Actions takes nothing returns nothing
    call BJDebugMsg("A unit entered the region!")
endfunction

//===========================================================================
function InitTrig_UnitsEnterRegions takes nothing returns nothing    
    call TriggerAddAction( gg_trg_UnitsEnterRegions, function Trig_UnitsEnterRegions_Actions )
endfunction
 
Last edited:
Top