HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Textmacro problems

02-27-2009, 10:16 AM#1
snowtiger
The problem is that all the variables get the same value, the one from the last RunTextMacro. Any ideas how to fix this?

My structs:
Collapse JASS:
struct AtRePoints
    point Attack = 0
    point Retreat = 0
endstruct

struct point
    real X = 0.
    real Y = 0.
endstruct

struct AIHero
    boolean Retreating = false                                  //Used to flag a retreating hero (because his hp is low)
    boolean Waiting = true                                      //Used to make sure the hero waits for a wave.
    unit hero = null                                            //The actual unit this struct is representing.
    integer nrAllies = 0                                        //Stores the number of allies near the hero.
    player owner
    point retreatPoint                                          //The point the hero should move to when retreating, should change.
    point attackPoint                                           //The point where the hero should attack-move to, should change often.
    boolean leftBase                                            //Is the hero from the left team?
    boolean ShouldControl = false                               //True if the hero is controlled by a pc, so we should control it (default false).
    


    method updateNrAllies takes nothing returns nothing         //Updates the number of allies near the hero.
        local group unitsInRange = CreateGroup()
        set tempPlayer = this.owner
        set tempCount = 0
        call GroupEnumUnitsInRangeOfLoc(unitsInRange,GetUnitLoc(this.hero),750.,null)
        call ForGroup(unitsInRange,function updateCount)
        set this.nrAllies = tempCount
        call DestroyGroup( unitsInRange)
    endmethod
    
    method stopWaiting takes nothing returns nothing            //Stops the hero from waiting for creeps, probably because there are enough creeps near him.
        set this.Waiting = false
        call IssuePointOrder(this.hero,"attack",this.attackPoint.X,this.attackPoint.Y)  
        debug call BJDebugMsg("Player " + I2S(GetPlayerId(this.owner ) + 1) + ": Stop waiting!" )      
    endmethod
    
    method fight takes nothing returns nothing                  //This makes the hero go back and fight, probably because he healed.
        set this.Retreating = false
        call IssuePointOrder(this.hero,"attack",this.attackPoint.X,this.attackPoint.Y)
        debug call BJDebugMsg("Player " + I2S(GetPlayerId(this.owner ) + 1) + ": Fight!" )
    endmethod
    
    method retreat takes nothing returns nothing
        set this.Retreating = true
        set this.Waiting = false                                //If a hero retreats, he shouldn't be waiting for a wave.
        call IssuePointOrder(this.hero,"move",this.retreatPoint.X,this.retreatPoint.Y)
        debug call BJDebugMsg("Player " + I2S(GetPlayerId(this.owner ) + 1) + ": Retreat!" )
    endmethod
    
    method wait takes nothing returns nothing                   //This hero will be waiting for an army to follow him in battle, as a lone hero can't do much.
        if not this.Retreating then                             //Prevent a low-hp hero to wait for creeps while running back.
            set this.Waiting = true                             //Flag this hero as waiting.
            call IssueImmediateOrder(this.hero, "stop")
            debug call BJDebugMsg("Player " + I2S(GetPlayerId(this.owner ) + 1) + ": Wait!" )
        debug else
            debug call BJDebugMsg("Player " + I2S(GetPlayerId(this.owner ) + 1) + ": Don't wait! Retreat!" )
        endif
    endmethod
endstruct

The Textmacro's
Collapse JASS:
function Trig_Hero_AI_Region_Init_Actions takes nothing returns nothing
        
    local AtRePoints ARP = AtRePoints.create()
    local point A = point.create()
    local point R = point.create()
  
    //! textmacro AddRect takes  rect, Color, AX, AY, RX, RY
    set Regions[numberOfRegions] = CreateRegion()
    call RegionAddRect( Regions[numberOfRegions], gg_rct_$Color$_$rect$)
    set A.X = $AX$
    set A.Y = $AY$
    set R.X = $RX$
    set R.Y = $RY$
    set ARP.Attack = A
    set ARP.Retreat = R
    set ARPoints[0][numberOfRegions] = ARP
    set ARP.Attack = R
    set ARP.Retreat = A
    set ARPoints[1][numberOfRegions] = ARP
    set numberOfRegions = numberOfRegions + 1
    //! endtextmacro
    
    //init regions
    
    //! runtextmacro AddRect("1","Yellow", "-3500." ,"1500." ,"-6666." ,"1000.")               
    //! runtextmacro AddRect("2","Yellow", "-3000.","1000.","-5000.","2500.")
    //! runtextmacro AddRect("3","Yellow", "-2000.","-500.","-3500.","1500.")
    //! runtextmacro AddRect("4","Yellow", "-500.","-1500.","-3000.","1000.")
    //! runtextmacro AddRect("5","Yellow", "2000.","-3000.","-2000.","-500.")
    //! runtextmacro AddRect("6","Yellow", "6000.","-3000.","-500.","-1500.")
    //! runtextmacro AddRect("7","Yellow", "6666.","-500.","2000.","-3000.")
    //! runtextmacro AddRect("1","Green", "-4000.","150.","-5000.","-500.")
    //! runtextmacro AddRect("2","Green", "-3500.","-500.","-5000","500")
    //! runtextmacro AddRect("3","Green", "-3500.","-1500.","-4000.","150.")
    //! runtextmacro AddRect("4","Green", "0.","3000.","-3500.","-500.")
    //! runtextmacro AddRect("5","Green", "3500.","-1666","-3500.","-1500.")
    //! runtextmacro AddRect("6","Green", "3000.","2500.","-3000.","2777.")
    //! runtextmacro AddRect("7","Green", "3500.","-500.","0.","3000.")
    //! runtextmacro AddRect("8","Green", "4000.","500.","3500.","-1666.")
    //! runtextmacro AddRect("9","Green", "5000.","500.","3500.","-500.")
    //! runtextmacro AddRect("10","Green", "5000.","1000.","4000.","500.")
    //! runtextmacro AddRect("1","Purple", "-2300.","-3000.","-6500.","-1000.")
    //! runtextmacro AddRect("2","Purple", "0.","0.","-6000.","-3000.")
    //! runtextmacro AddRect("3","Purple", "2000.","500.","-2300.","-3000.")
    //! runtextmacro AddRect("4","Purple", "3300.","800.","0.","0.")
    //! runtextmacro AddRect("5","Purple", "4000.","1500.","2000.","500.")
    //! runtextmacro AddRect("6","Purple", "5500.","3500.","3300.","800.")
    //! runtextmacro AddRect("7","Purple", "6500.","500.","4000.","1500.") //this value overwrites the others somehow
endfunction   
   

Sorry if I didn't give enough information, just ask for it, I just didn't want to make this post flooded with code ;)

EDIT: It's the ARPoints[][] that messes up, it's a global array that stores the values.
02-27-2009, 10:27 AM#2
saw792
You only create the structs once, then you set their value every time you run the textmacro. Of course they are going to overwrite eachother.

Also, why did you invent this point object? That is what 'location' is for.
02-27-2009, 10:44 AM#3
snowtiger
Quote:
Originally Posted by saw792
You only create the structs once, then you set their value every time you run the textmacro. Of course they are going to overwrite eachother.
Yeah,but I store their values in the array, is that not good enough?

Quote:
Also, why did you invent this point object? That is what 'location' is for.
sure, but personnally I like point more, lol. :)

Thanks for your help.
02-27-2009, 12:45 PM#4
saw792
You don't store their values in the array. What you are actually storing is an array index (that is what a struct object really is). The struct index never changes because you only declare one struct instance. That means that when you refer to ARPoints[0][numberOfRegions] it will give you back an array index, which then points to a set of variables with that array that have all been updated since they were stored.
02-27-2009, 01:10 PM#5
snowtiger
Oh, I (obviously :D) didn't know that. So, any idea what I should do? I just started vJassing so I don't have many experience with it, but hey, learning is the goal! :D
02-27-2009, 02:03 PM#6
Vexorian
what's the part in which you declare ARPoints ?
02-27-2009, 03:08 PM#7
snowtiger
Oh sorry, that's a global.

Collapse JASS:
globals
    region array Regions[100]
    integer numberOfRegions = 0                  //the total numbers of regions we have, will be set while initing the regions
    AtRePoints array ARPoints[2][100] = 0
endglobals
02-27-2009, 03:15 PM#8
Vexorian
well, yeah a struct variable is a pointer and does not hold the actual struct, just a reference to it, imagine it as an arrow pointing to the actual struct. So when you assign a struct variable to another, you are just making it point to something else, you are only copying the arrow, not the struct itself.

So a workaround I can think of:

Collapse JASS:
struct AtRePoints
    point Attack = 0
    point Retreat = 0

    method clone takes nothing returns AtRePoints
     local AtRePoints r = AtRePoints.create()
        set r.Attack = this.Attack.clone()
        set r.Retreat = this.Retreat.clone()
     return r
    endmethod
endstruct

struct point
    real X = 0.
    real Y = 0.

    method clone takes nothing returns point
     local point r = point.create()
        set r.X = this.X
        set r.Y = this.Y
     return r
    endmethod

endstruct

//Then, in the text macro:

   set ARPoints[0][numberOfRegions] = ARP.clone()
    set ARP.Attack = R
    set ARP.Retreat = A
    set ARPoints[1][numberOfRegions] = ARP.clone()


Of course, this is just a quick work around, you could modify the textmacro itself so it creates the new instances automatically, maybe that's cleaner.


Edit:
Collapse JASS:
  
    //! textmacro AddRect takes  rect, Color, AX, AY, RX, RY
    set Regions[numberOfRegions] = CreateRegion()
    call RegionAddRect( Regions[numberOfRegions], gg_rct_$Color$_$rect$)
    set A=point.create()
    set R=point.create()
    set A.X = $AX$
    set A.Y = $AY$
    set R.X = $RX$
    set R.Y = $RY$
    set ARP= AtRePoint.create()
    set ARP.Attack = A
    set ARP.Retreat = R
    set ARPoints[0][numberOfRegions] = ARP
    set ARP= AtRePoint.create()
    set ARP.Attack = R
    set ARP.Retreat = A
    set ARPoints[1][numberOfRegions] = ARP
    set numberOfRegions = numberOfRegions + 1
    //! endtextmacro
    
02-27-2009, 04:54 PM#9
snowtiger
Thanks a lot! Works fine now. And learned something today ;)
+rep.