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

[JASS] vjASS [Structs] - no initial value

Status
Not open for further replies.
Hey all.
I have a little question.

I am having a struct A.
Before this struct, I declare a private global constant (PGC).
Inside the struct I set a member to the PGC,
but its 0. How does that come, if the PGC is not 0?

JASS:
globals
    private constant real EXAMPLECONSTANT = 128.
endglobals

struct ExampleStruct
    real MEMBER = EXAMPLECONSTANT
endstruct

This (changed) doesn't work in my struct... Its always after creation 0. But not if I set during creation.
 
Nope...
Well, I declared other variables, but those have values.
I just want to know how its possible to get a 0 if its actually
not null, and that for some constant global variables.

The whole code is up to 1000 lines and not finished, so its
a monster of code, but I did comment it alot. But finally, I want
to release it when its ready. I just want to know when it can
happen that constants are not initializing a member of a struct, by
creating a new instance.
 
Level 8
Joined
Aug 4, 2006
Messages
357
Anachron, can you post the code that is telling you MEMBER is 0? I did a little test using your code and found that MEMBER is set to 128. Here:
JASS:
scope ExampleScope initializer Init
globals
    private constant real EXAMPLECONSTANT = 128.
endglobals

struct ExampleStruct
    real MEMBER = EXAMPLECONSTANT
endstruct
function Init takes nothing returns nothing
    local ExampleStruct ex = ExampleStruct.create()
    call BJDebugMsg("MEMBER is "+R2S(ex.MEMBER)) //output: "MEMBER is 128.000"
endfunction
endscope
 
Level 11
Joined
Apr 6, 2008
Messages
760
Constants get inlined

JASS:
globals
constant real Y = 128.
endglobals

struct A
    real X = Y
endstruct

struct B
    real X = 128. //this is the same thing as above
endstruct


function Action takes nothing returns nothing
    local A a = A.create() //when i call this the X will be set to what ever value that is after the =
    
    //But u could also
    set a.X = Y  
    //this is the same thing as doing like u want
    //since when u create it it allocate a struct index and get the X get set to Y(128.), so same shit
endfunction
 
Level 8
Joined
Aug 4, 2006
Messages
357
Anachron, would you mind showing us how you know what works and what doesn't? Are you using BJDebugMsg? If you look at the code I posted above, I proved that struct members are assigned when the struct instance is created. Show us the code you use to test this stuff and its exact output.
 
Level 17
Joined
Mar 17, 2009
Messages
1,350
Well you can't give a struct's member an initial value (so I know, I might be wrong about this), I mean the best way to give it a value would be setting it after creating the struct:
JASS:
library DT initializer blah
globals

    private constant real NUMB = 28.
    
endglobals

struct DEU

    real r
    
endstruct

private function test takes nothing returns nothing
    local DEU d = DEU.create()
    set d.r = NUMB
    call DisplayTextToForce(GetPlayersAll(), R2S(d.r))
endfunction


//===========================================================================
private function blah takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterTimerEventPeriodic(t, 2.)
    call TriggerAddAction(t, function test)
endfunction
endlibrary

Test this and you'll see that it works just as supposed to.
 
Level 8
Joined
Aug 4, 2006
Messages
357
As I proved earlier, you can assign struct members initial values like real powerLevel = 9001. I am absolutely sure this works. You must be having a problem in how you test the values of your struct members... Just post your testing script when you get a chance, so we can find the problem.
 
Level 9
Joined
Nov 28, 2008
Messages
704
Or as someone mentioned (I think?) just set them inside the create method. The method masked is talking about movies it into the create function anyways, so it doesn't matter, just looks prettier ;).
 
Level 17
Joined
Mar 17, 2009
Messages
1,350
maskedpoptart said:
As I proved earlier, you can assign struct members initial values like real powerLevel = 9001. I am absolutely sure this works. You must be having a problem in how you test the values of your struct members... Just post your testing script when you get a chance, so we can find the problem.
When I do that, the map doesn't run... but when I set a value later in a function, it works...
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,285
Static struct variables are initialized on map init and are equivently constants.
Struct variables are initilized on the creation of a struct insance and can take preset values.

What may be happening is the actual compiler is messing up and not representing it properly. As I heard all constant non handles replace the references with the actual number on save for speed.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,285
JASS:
library TestStruct initializer init
    globals
        private constant real TESTREAL = 128
    endglobals

    private struct TSTR
        public real a = TESTREAL
        public real b = 128
    endstruct
    
    private function init takes nothing returns nothing
        local TSTR tester = TSTR.create()
        call BJDebugMsg(R2S(tester.a))
        call BJDebugMsg(R2S(tester.b))
        call tester.destroy()
    endfunction
endlibrary

Code:
library TestStruct initializer init
    globals
        private constant real TESTREAL = 128
    endglobals

    private struct TSTR
        public real a = TESTREAL
        public real b = 128
    endstruct
    
    private function init takes nothing returns nothing
        local TSTR tester = TSTR.create()
        call BJDebugMsg(R2S(tester.a))
        call BJDebugMsg(R2S(tester.b))
        call tester.destroy()
    endfunction
endlibrary

This displays 128.000 for both a and b. Thus I have no clue what you are complaining about as it works perfectly for me.
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
Some scenarios:

JASS:
globals
real const=128.
endglobals
struct s
real val=const
endstruct

JASS:
globals
real const=128.


//JASSHelper struct globals:
constant integer si__s=1
integer si__s_F=0
integer si__s_I=0
integer array si__s_V
real array s__s_val

endglobals


//Generated allocator of s
function s__s__allocate takes nothing returns integer
 local integer this=si__s_F
    if (this!=0) then
        set si__s_F=si__s_V[this]
    else
        set si__s_I=si__s_I+1
        set this=si__s_I
    endif
    if (this>8190) then
        return 0
    endif

   set s__s_val[this]=const
    set si__s_V[this]=-1
 return this
endfunction

//Generated destructor of s
function s__s_destroy takes integer this returns nothing
    if this==null then
        return
    elseif (si__s_V[this]!=-1) then
        return
    endif
    set si__s_V[this]=si__s_F
    set si__s_F=this
endfunction

JASS:
globals
constant real const=128.
endglobals
struct s
real val=const
endstruct

JASS:
globals
constant real const=128.


//JASSHelper struct globals:
constant integer si__s=1
integer si__s_F=0
integer si__s_I=0
integer array si__s_V
real array s__s_val

endglobals


//Generated allocator of s
function s__s__allocate takes nothing returns integer
 local integer this=si__s_F
    if (this!=0) then
        set si__s_F=si__s_V[this]
    else
        set si__s_I=si__s_I+1
        set this=si__s_I
    endif
    if (this>8190) then
        return 0
    endif

   set s__s_val[this]=const
    set si__s_V[this]=-1
 return this
endfunction

//Generated destructor of s
function s__s_destroy takes integer this returns nothing
    if this==null then
        return
    elseif (si__s_V[this]!=-1) then
        return
    endif
    set si__s_V[this]=si__s_F
    set si__s_F=this
endfunction

JASS:
struct s
real val=128.
endstruct

JASS:
globals
//JASSHelper struct globals:
constant integer si__s=1
integer si__s_F=0
integer si__s_I=0
integer array si__s_V
real array s__s_val

endglobals


//Generated allocator of s
function s__s__allocate takes nothing returns integer
 local integer this=si__s_F
    if (this!=0) then
        set si__s_F=si__s_V[this]
    else
        set si__s_I=si__s_I+1
        set this=si__s_I
    endif
    if (this>8190) then
        return 0
    endif

   set s__s_val[this]=128.
    set si__s_V[this]=-1
 return this
endfunction

//Generated destructor of s
function s__s_destroy takes integer this returns nothing
    if this==null then
        return
    elseif (si__s_V[this]!=-1) then
        return
    endif
    set si__s_V[this]=si__s_F
    set si__s_F=this
endfunction

The moral: You're doing it wrong.
 
Edit: Yes, it works. But not for my struct... Very VERY strange...
Edit2: Oh btw, I am having everything inside a scope..

Edit: Some code:

JASS:
scope WhispWheels
    globals
        //#==============================#
        //>         System Settings      <
        //#==============================#
        private constant real BASE_RANGE = 250.
        // ^ Sets the default range for whisp roots
    endglobals

struct WhispWheel
        //#=======================================#
        //>  The normal members of the struct     <
        //#=======================================#
        real range = BASE_RANGE

        public static method create takes real x, real y, player p returns WhispWheel
            // ################################
            // #            Locals            #
            // ################################
            local WhispWheel ww = WhispWheel.allocate()
            // ^ the instance object
            
            // ################################
            
            call BJDebugMsg(R2S(ww.range))
            return ww
        endmethod
endstruct
endscope
 
Edited the post above.

Edit... (lol): It does work there... What the hell..
Edit2:
JASS:
//Generated allocator of WhispWheel
function s__WhispWheel__allocate takes nothing returns integer
 local integer this=si__WhispWheel_F
    if (this!=0) then
        set si__WhispWheel_F=si__WhispWheel_V[this]
    else
        set si__WhispWheel_I=si__WhispWheel_I+1
        set this=si__WhispWheel_I
    endif
    if (this>-1) then
        return 0
    endif
   set s__WhispWheel_range[this]= WhispWheels__BASE_RANGE
 return this
endfunction
Seems like everything is working on WE..
 
JASS:
// The Constructor-Method. Does return the new instance
        public static method create takes real x, real y, player p returns WhispWheel
            // ################################
            // #            Locals            #
            // ################################
            local WhispWheel ww = WhispWheel.allocate()
            // ^ the instance object
            local integer i = 0
            // ^ a local counter for the loop
            local real f = 0.
            // ^ a local degrees for roots
            local Loc l = 0
            // ^ a local struct of the location.
            // This is only to be able to get X and
            // Y back at the same time
            local Root r = 0
            // ^ a local struct of the root which we
            //     will add to the whispwheels
            local real rootX = 0.
            local real rootY = 0.
            // ^ ^ The two coordinates for the new Root
            
            set ww.wheel = CreateUnit(p, ID_WHEEL, x, y, 270.)
            // ^ create the Whisp Wheel Unit
            
            // ################################
            
            call BJDebugMsg(R2S(ww.range))
            
            // Add basedata to the instance
            set ww.x = x
            set ww.y = y
            
            // Now lets add the Base-Roots to the Wheel
            loop
                exitwhen i >= BASE_ROOTS
                set f = f + 360 / BASE_ROOTS
                //call BJDebugMsg("F: " + R2S(f))
                // ^ This sets the degrees symetric,
                //   whatever the amount of baseroots are
                
                set l = Loc.new(x, y)
                set l = Loc.polarOffset(l, f, BASE_RANGE)
                set rootX = l.getX()
                set rootY = l.getY()
                // ^ gets the new coordinates for the root
                call l.destroy()
                // ^ destroy the helper-array
                set r = Root.create(rootX, rootY, f, p)
                call ww.addRoot(r)
                // ^ Creates the new Root-Instance
                // and adds it to the whispwheel
                set i = i + 1
            endloop
            
            // Check for attaching this instance to the struct
            if AUTO_ADD then
                call WhispWheel.addIns(ww)
            endif
            
            //set defaults
            //set ww = WhispWheel.setDefaults(ww)
            
            return ww
        endmethod

Thats 100% of the method...
 
Status
Not open for further replies.
Top