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

Globals initialized by natives

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

Why doesn't this line work?

JASS:
constant integer FARMER_INDEX = GetUnitPointValueByType(FARMER_ID) - NPC_CONS

NPC_CONS = 300 and the point value of FARMER_ID is equal to 304. But when I call FARMER_INDEX, it is equal to -300. Why can't I make a global like this...
 
Alright how do I make it work the way I want to then? There has to be a workaround???

You can't, unless you lose the constant keyword:
JASS:
library Test

    globals
        integer FARMER_INDEX
    endglobals

    private module Init 
        private static method onInit takes nothing returns nothing
            set FARMER_INDEX = GetUnitPointValueByType(FARMER_ID) - NPC_CONS 
        endmethod
    endmodule

    struct Example
        implement Init
    endstruct

endlibrary

If you are curious why I used a module, see the "Initialization" part here:
http://www.hiveworkshop.com/forums/...80/jpag-jass-proper-application-guide-204383/

Otherwise, you can always do something like:
JASS:
constant integer FARMER_INDEX = 304 - NPC_CONS
Obviously, that isn't what you want though.
 
Level 15
Joined
Aug 7, 2013
Messages
1,338
I ended up just making it a static integer field of the struct, and then initialized it's value in an init, so it's all good. I guess it's true you can't initialize globals like that. For some reason passing negative indices to the array actually gave back 'usable' values (as in it returned a struct strangely that did stuff). I don't recall JASS arrays allowing negative indices (unlike a language like Python though).
 
Level 15
Joined
Aug 7, 2013
Messages
1,338
Actually it does seem like negative indices are really allowed (somebody posted about that)... but I think it simply returns a value from the highest index so -1 would be 1 index below the max I think


That is how they work in Python, -1 should refer to the last member in the array...good to know negative indices do work though.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
If you are curious why I used a module, see the "Initialization" part here:
I thought it was just because you are addicted to structs when such a problem probably does not require one. Since it is no longer constant the value could be initialized at any time, as long as before it is used. Map initialization is probably best (if it works) but not necessary.
 
That is how they work in Python, -1 should refer to the last member in the array...good to know negative indices do work though.

Be careful, I don't think anyone has tested the behavior of negative indices for all forms of jass array.

Consider:

JASS:
globals
    private integer array braap[4]
endglobals

for x = 0 -> 3, set braap[x] = x
print braap[-1]

compared to:

JASS:
globals
    private integer array dyn
endglobals

for x = 0 -> 3, set dyn[x] = x
print dyn[-1]

Indeed, the maximum size of an array in jass is not the same thing as the highest instantiated member of an array in jass.

Edit: WARNING: Don't use this behavior for referencing struct instances on a stack. Once an array index is instanciated, it can't be uninstanciated.

Consider:

JASS:
globals
    private integer array dyn
endglobals
set dyn[3] = 0 //internally instantiates the array's size to ceil(lg(3+1))^2 = 4, but values 0,1,2 are undefined (not null, not 0, etc)
print dyn[1] //prints nothing
print dyn[5] //prints nothing
print dyn[3] //prints 0

If you're used to real programming languages, this behavior is more like an arraylist
 
Status
Not open for further replies.
Top