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

Loops with textmacros?

Status
Not open for further replies.
Level 15
Joined
Aug 7, 2013
Messages
1,338
Hi,

Is there anyway to do loops with textmacros?

e.g. instead of this

JASS:
//! runtextmacro RegionFilter("p1", "0")
//! runtextmacro RegionFilter("p2", "1")
//! runtextmacro RegionFilter("p3", "2")
//! runtextmacro RegionFilter("p4", "3")
...

do this

JASS:
for i in range 0 to TOTAL_PLAYERS do
  //! runtextmacro RegionFilter("p" + I2S(i + 1), I2S(i))
endfor

???

Also, is there a way I can modify the values of passed arguments? E.g. say I wanted this

JASS:
//! runtextmacro RegionFilter("0")

but I want the function name to be "p1RegionFilter" instead of "p0RegionFilter." Can I somehow turn the "0" into an int, add 1 to it, then cast back to a string?

Finally, can I use textmacro to fill up arrays?

JASS:
//some text macro command
loop
  exitwhen i == TOTAL_FUNCTIONS
  set myArray[i] = Filter($text macro function of i$)
  set i  = i + 1
endloop

That way I don't need to have 12 lines of setting an array...I mean they will still be there when translated, but it looks cleaner on readability side.
 
You don't need to loop textmacroes like that.
JASS:
//! textmacro example takes I
    set $I$ = 0
    loop
        call BJDebugMsg(I2S($I$))
        exitwhen $I$ == bj_MAX_PLAYERS
        set $I$ = $I$ + 1
    endloop
//! endtextmacro


function Trig_Melee_Initialization_Actions takes nothing returns nothing
    local integer i
    //! runtextmacro example("i")
endfunction

//===========================================================================
function InitTrig_Melee_Initialization takes nothing returns nothing
    set gg_trg_Melee_Initialization = CreateTrigger(  )
    call TriggerAddAction( gg_trg_Melee_Initialization, function Trig_Melee_Initialization_Actions )
endfunction
Display 0,1,2,...,12 on the screen.

Everything you send into textmacro act like a string.
All $I$ will be replaced with whatever string you send, it this case i.
So your code actually look like:
JASS:
function Trig_Melee_Initialization_Actions takes nothing returns nothing
    local integer i
    set i = 0
    loop
        call BJDebugMsg(I2S(i))
        exitwhen i == bj_MAX_PLAYERS
        set i = i + 1
    endloop
endfunction

//===========================================================================
function InitTrig_Melee_Initialization takes nothing returns nothing
    set gg_trg_Melee_Initialization = CreateTrigger(  )
    call TriggerAddAction( gg_trg_Melee_Initialization, function Trig_Melee_Initialization_Actions )
endfunction
 
1. jasshelper doesn't have too many compile-time features. And textmacros can't be nested, so I don't think what you want is possible. There might be room to make it cleaner, but it all depends on the macro's code.

2. No, you'll have to add a separate parameter for that in that case, like:
JASS:
//! textmacro RegionFilter takes NUM1, NUM2
    set myArray[$NUM1$] = Filter(function p$NUM2$RegionFilter)
//! endtextmacro

//! runtextmacro RegionFilter("0", "1")

(again, jasshelper is limited in compile-time stuff)

3. Yes, you can. I recommend using a global integer and setting it to 0 right before the loop starts. Something like this:
JASS:
library ArrayUtility

    globals
        public integer iterator = 0
    endglobals

    //! textmacro FillArray takes NAME, SIZE, VALUE
        set ArrayUtility_iterator = 0
        loop
            exitwhen ArrayUtility_iterator == $SIZE$
            set $NAME$[ArrayUtility_iterator] = $VALUE$
            set ArrayUtility_iterator = ArrayUtility_iterator + 1
        endloop
    //! endtextmacro

endlibrary

Eh, but it really isn't that dynamic. I mean, they'll all end up with the same value, defined by $VALUE$ (I think you wanted them to change?). It might be useful to set things to null or to set them to an initial value, but that is about it. It is still limited by the rules of JASS, so you can't really abuse it much. Judging by your last statement:
That way I don't need to have 12 lines of setting an array...I mean they will still be there when translated, but it looks cleaner on readability side.
I'm guessing you're trying to avoid:
JASS:
set myArray[0] = Filter(function A0)
set myArray[1] = Filter(function A1)
set myArray[2] = Filter(function A2)
...
Sadly, you can't really avoid that since JASS doesn't allow you to mess around with function names in the way you desire. :(
 
Status
Not open for further replies.
Top