• 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.
  • Create a faction for Warcraft 3 and enter Hive's 19th Techtree Contest: Co-Op Commanders! Click here to enter!
  • Create a void inspired texture for Warcraft 3 and enter Hive's 34th Texturing Contest: Void! Click here to enter!
  • The Hive's 21st Texturing Contest: Upgrade is now concluded, time to vote for your favourite set of icons! Click here to vote!

structs question

Status
Not open for further replies.
Level 13
Joined
Jan 2, 2016
Messages
978
If I do this:
JASS:
struct a
    integer i = 0
endstruct
would it be the same as this:
JASS:
struct a
    integer i

    static method create takes nothing returns thistype
        local thistype this = thistype.allocate()
        set this.i = 0
        return this
    endmethod
endstruct
Or would the 1-st way not always have i set to 0 upon creation? xP
 

LeP

LeP

Level 13
Joined
Feb 13, 2008
Messages
545
You could always just look at the generated code. It's in jngp/logs/output{,_}war3map.j
JASS:
struct foo
    integer x = 7
endstruct
becomes
JASS:
//Generated allocator of foo
function s__foo__allocate takes nothing returns integer
 local integer this=si__foo_F
    if (this!=0) then
        set si__foo_F=si__foo_V[this]
    else
        set si__foo_I=si__foo_I+1
        set this=si__foo_I
    endif
    if (this>8190) then
        call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,1000.,"Unable to allocate id for an object of type: foo")
        return 0
    endif

   set s__foo_x[this]= 7
    set si__foo_V[this]=-1
 return this
endfunction

As you can see it actually set's the value in the allocator so this would work too:
JASS:
struct foo
    integer x = 7
  
    static method create takes nothing returns thistype
        local thistype this = allocate()
        call BJDebugMsg(I2S(this.x)) // prints 7
        return this
    endmethod
endstruct
 
Status
Not open for further replies.
Top