• 🏆 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!

Undefined variable behavior why?

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
I have this struct:

JASS:
struct City 
        integer array buildingCount_tot[BUILDING_TYPES_MAX] 
        integer array buildingCount_wip[BUILDING_TYPES_MAX]
        Menu    menu 
        
        static integer count = 1
    
        static method create takes string s, rect r returns City
            local thistype c = .allocate()
            local integer i = 0
            set c.menu = Menu.create(c)
            set City.count = City.count + 1
            loop                                                 // Here they are initialized to 0!
                set c.buildingCount_tot[i] = 0
                set c.buildingCount_wip[i] = 0
                set i = i + 1 
                exitwhen i == BUILDING_TYPES_MAX
            endloop
            // Add To Damage Trigger 
            return c
        endmethod 

method addBuildingCountTot takes integer building, integer amount returns nothing 
            set .buildingCount_tot[building] = .buildingCount_tot[building] + amount
        endmethod
        
        method addBuildingCountWip takes integer building, integer amount returns nothing 
            set .buildingCount_wip[building] = .buildingCount_wip[building] + amount
        endmethod

then when I increase the count in another function...
JASS:
private function OnConstructionStart takes nothing returns boolean
        local unit u = GetTriggerUnit()
        local integer uid = GetUnitUserData(u)
        local integer pid = GetPlayerId(GetOwningPlayer(u))
        local integer b = BT.getBuildingIndexByUnit(u)
        local integer i = curSelection[pid]
        set owningCity[uid] = i
        call city[i].addBuildingCountWip(b, 1)
        call city[i].addBuildingCountTot(b, 1)
        // refresh this building
        call BJDebugMsg(I2S(b))
        call BJDebugMsg("Wip: " + I2S(city[i].buildingCount_wip))
        call BJDebugMsg("Tot: " + I2S(city[i].buildingCount_tot))
        set u = null
        return false 
    endfunction

The city.buildingCount_wip is constantly 120 in one city, 30 in another and so on. Its supposed to count buildings but the buildingCount don't change. Don't get it.

Removed most parts that are not relevant to the question.
 
Level 12
Joined
Feb 22, 2010
Messages
1,115
Maybe show what exactly are getBuildingIndexByUnit, curSelection and owningCity.
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
Maybe show what exactly are getBuildingIndexByUnit, curSelection and owningCity.

No need, it is something with the syntax I think. I decided to change it to this which works. Couldn't be bothered with the arrays. ;p

JASS:
struct City 
        static integer array buildingCountTot
        static integer array buildingCountWip

        method addBuildingCountTot takes integer building, integer amount returns nothing 
            local integer i = building + ((BT.count)*(.index - 1))
            set City.buildingCountTot[i] = City.buildingCountTot[i] + amount
        endmethod
        
        method addBuildingCountWip takes integer building, integer amount returns nothing 
            local integer i = building + ((BT.count)*(.index - 1))
            set City.buildingCountWip[i] = City.buildingCountWip[i] + amount
        endmethod
        
        method getBuildingCountTot takes integer building returns integer 
            return City.buildingCountTot[building + ((BT.count)*(.index - 1))]
        endmethod
        
        method getBuildingCountWip takes integer building returns integer 
            return City.buildingCountWip[building + ((BT.count)*(.index - 1))]
        endmethod
 
Status
Not open for further replies.
Top