HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

[vJASS] methods takes variable?

09-05-2008, 06:07 AM#1
Zandose
Is there some way for a method to take a variable (see highlight) which can be either 'a' or 'b'. So far I'm just using a number (a is 1 and b is 2) and if statements, but I need to make copies of the code for both a and b.

This:
Collapse JASS:
struct test
     integer a = 100
     integer b = 200
     method something takes integer i returns nothing
          call BJDebug(I2S(i)) //i equals either 'this.a' or 'this.b'
     endmethod
endstruct

Instead of this:
Collapse JASS:
struct test
     integer a = 100
     integer b = 200
     method something takes integer i returns nothing
          if i == 1 then
               call BJDebug(I2S(this.a))
          elseif i == 2 then
               call BJDebug(I2S(this.b))
          endif
     endmethod
endstruct
09-05-2008, 07:09 AM#2
Spec
You can use arrays instead of 2 variables in structure:
Collapse JASS:
struct test
  integer array c[2]
  // . . .
  method something takes integer i returns nothing
    call BJDebugMsg(I2S(c[i-1]))
  endmethod
endstruct
This needs variable initialization and also this have some risk, that "i" would be >2 or <1

Or, maybe, would be simplier to make method for each variable (if there are only two of them, that making difference between actions):
Collapse JASS:
struct test
  integer a = 100
  integer b = 200

  //! textmacro testmethods takes var
  method something$var$ takes nothing returns nothing
    call BJDebugMsg(I2S(.$var$))
  endmethod
  // . . .
  //! endtextmacro

  //! runtextmacro testmethods("a")
  //! runtextmacro testmethods("b")
endstruct
09-05-2008, 12:08 PM#3
Gwypaas
Read the interface part in the manual I think that will help you.
09-05-2008, 12:10 PM#4
Zandose
Sorry my example was not clear enough (didn't think of it), but each variable is already a array.

Collapse JASS:
struct test
     integer array a[10] = 100
     integer array b[10] = 200
     method something takes integer i returns nothing
          call BJDebug(I2S(i)) //i equals either 'this.a' or 'this.b'
     endmethod
endstruct

09-05-2008, 12:25 PM#5
Vexorian
I really think what you want to do is non-sense anyway:

Collapse JASS:
type myUltraArray extends integer array [ 10]

struct test
    myUltraArray a = 0
    myUltraArray b = 0

    method something takes myUltraArray x returns nothing
        set x[2]=0
    endmethod

    static method create takes nothing returns test
     local test t=test.allocate()
        set t.a=myUltraArray.create()
        set t.b=myUltraArray.create()
     return t
    endmethod

    private method onDestroy takes nothing  returns nothing
        call this.a.destroy()
        call this.b.destroy()
    endmethod

endstruct

oh:

Collapse JASS:
struct test
     integer array a[10] = 100 //how to make array members fail in vJass.
     integer array b[10] = 200 //what exactly do you think this does? I would say it doesn't do what you think...
09-05-2008, 12:26 PM#6
Anitarf
Well, if you only have a and b, the simplest way to do this is with duplication of code. If you have more members, though, I suggest an array of dynamic arrays.
09-06-2008, 12:32 AM#7
Zandose
I think I've messes up again in explaining what I want. Simply (see code): x becomes/represent this.a, this.b, or this.c.

Collapse JASS:
type myUltraArray extends integer array [10]

struct test
    myUltraArray a
    myUltraArray b
    myUltraArray c

        //I want "x" to represent ".a", ".b", or ".c". 
        //For instance, if for "myUltraArray x" someone used ".a" or just "a" I want "x" to become/represent ".a", ".b", ".c", etc.
    method something takes myUltraArray x returns nothing
        set x[2][1]=0 
    endmethod
    
    static method onInit takes nothing returns nothing
        local test t = test.create()
        set t.a = myUltraArray.create()
        set t.b = myUltraArray.create()
        set t.c = myUltraArray.create()
    endmethod
endstruct
09-06-2008, 01:37 AM#8
Vexorian
My example does that already... except of course it won't magically turn a dynamic array into 2d...