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

[vJASS] string array appears to be static

Status
Not open for further replies.
Level 24
Joined
Aug 1, 2013
Messages
4,657
JASS:
struct s
    private integer i
    private string array s[10]
    
    public method setI takes integer value returns nothing
        set i = value
    endmethod
    public method getI takes nothing returns integer
        return i
    endmethod
    public method setS takes integer index, string value returns nothing
        set this.s[index] = value
    endmethod
    public method getS takes integer index returns string
        return this.s[index]
    endmethod
    
    public static method getType takes integer i returns thistype
        local thistype this = i
        if this == null then
            set this = thistype.create()
        endif
        return this
    endmethod
endstruct

function Trig_Untitled_Trigger_001_Actions takes nothing returns nothing
    local s s1 = s.getType(1)
    local s s2 = s.getType(2)
    local s s3 = s.getType(3)
    
    call s1.setI(10)
    call s2.setI(11)
    call s3.setI(12)
    
    call BJDebugMsg("s1 = " + I2S(s1.getI()))
    call BJDebugMsg("s2 = " + I2S(s2.getI()))
    call BJDebugMsg("s3 = " + I2S(s3.getI()))
    
    call s1.setS(1, "bla1")
    call s2.setS(1, "bla2")
    call s3.setS(1, "bla3")
    
    call BJDebugMsg("s1 = " + s1.getS(1))
    call BJDebugMsg("s2 = " + s2.getS(1))
    call BJDebugMsg("s3 = " + s3.getS(1))
    
endfunction

The thing is that all 3 objects (s1, s2 and s3) have all a separate integer i, but they have a shared string array s.

As seen in the debug messages:
s1 = 10
s2 = 11
s3 = 12

s1 = bla3
s2 = bla3
s3 = bla3

So how do I make separate arrays?
 
If I remember correctly, array-struct-members actually require the instance to have been created through the struct's create or allocate method.

In your case, you have this:
JASS:
    public static method getType takes integer i returns thistype
        local thistype this = i
        if this == null then
            set this = thistype.create()
        endif
        return this
    endmethod
You're directly setting the instances to 1, 2, and 3. thistype.create() is never actually called. If you change that entire function to just return "thistype.create()", I assume it should work. If you need the integers to map directly to the instance, you can just use an extra array or just use a hashtable (if you want a mapping outside of the range 0-8191).
 
Status
Not open for further replies.
Top