HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Help with globals and library inits

07-26-2014, 01:30 PM#1
Yrth
this is gonna be a multi-parter, all help is much appreciated if you can only answer some!

in the vJASS help doc, Vex says that globals should not reference other globals in declarations

part 1. is does this apply to constants? (these examples are imagined as separate triggers)
Collapse A:
globals
    constant integer MaxNumberPlayers = 8
endglobals

Collapse B:
globals
    unit array Heroes[MaxNumberPlayers]
endglobals

part 2. does this apply if they're manually ordered using libraries?
Collapse A:
library A
globals
    integer x = 5
endglobals
endlibrary

Collapse B:
library B requires A
globals
    //i realize y would only capture x's initial value... 
    //handles (being sorta pointers) would have updated data visible to both globals... I think?
    integer y = x 
endglobals
endlibrary

q2: if nothing requires library A, do these do the exact same thing (in different ways)
Collapse Version 1:
library A initializer initA
globals
    unit array heroes[4]
endglobals

function initA takes nothing returns nothing
    set heroes[0] = ...
    ...
endfunction
endlibrary

Collapse Version 2:
globals
    unit array heroes[4]
endglobals

function InitTrig_A takes nothing returns nothing
    set heroes[0] = ...
    ...
endfunction

minor differences in this last section are important, especially each's limitations
07-26-2014, 04:20 PM#2
Bannar
Referencing other library globals should not be a problem if you properly build an "order tree" via requires/needs/uses.

If you do not, however, it might easily output error because object/scalar we are trying to referece to, is not yet defined. And yes, this would use only initial values.

If you want to make sure that parent's lib data is initialized especially when part of it can not be initialized directly within globals block use module initializer.
If you don't, it might happen that child struct with module initializer "extending" parent struct from another library may have it's data initialized prior to parent struct, thus providing false results.

Here (under "Initialization Priorities") you can find the proper order of all posible inits.
07-27-2014, 12:56 PM#3
Yrth
Thanks for the help, Inferno

Ok cool -- I didn't think it would pull the globals into the script header AND remember where it pulled them from :)

and ah that's a good guide, wish I had that when I was planning organization... so the only difference is in which resolves first?
is using cohadar's flavor of JASS the new standard?
07-27-2014, 07:19 PM#4
PurgeandFire111
Inferno hit the nail on the head. I just thought I'd jump in and mention that this:
Collapse JASS:
globals
    unit array heroes[4]
endglobals
The [4] doesn't do anything. All arrays in JASS have 8192 indexes. Adding a size declaration after an array is only useful in two cases: (1) if the global is a struct member array (2) if the size is > 8192 (it'll tell jasshelper to build two arrays, but you can code using the array as if it is one mega array).

You might've just been giving examples, but I figured I'd let you know.

Quote:
is using cohadar's flavor of JASS the new standard?

Cohadar's jasshelper fixes the initialization order. It would be considered "standard", but it has some random bugs, according to Nestharus. I use it sometimes, but there are some annoying parts of it (e.g. the //! external warning). If you download moyack's JNGP 2.0, you can switch between vex's and cohadar's jasshelper really easily through the jasshelper menu.

Anyway, it is a good habit to make use of libraries and its "requires" keyword. In fact, a lot of people ditch using scopes at all and just stick with libraries.
07-28-2014, 06:29 PM#5
Anitarf
I'd second Purge's suggestion of only using libraries, it's better to always explicitly write down the requirements of your code.

In fact, when Vexorian wrote Zinc, he made it only use libraries.
07-31-2014, 05:56 AM#6
Yrth
Quote:
Cohadar's jasshelper fixes the initialization order. It would be considered "standard", but it has some random bugs, according to Nestharus. I use it sometimes, but there are some annoying parts of it (e.g. the //! external warning). If you download moyack's JNGP 2.0, you can switch between vex's and cohadar's jasshelper really easily through the jasshelper menu.
I saw that. this new ver 2.0 is amazing. multiplayer emulation is crazy helpful... mostly on your friends
i haven't tried switching since it's only guaranteed to be compatible in one direction (going to cohadars)

Quote:
Originally Posted by Purge
Anyway, it is a good habit to make use of libraries and its "requires" keyword. In fact, a lot of people ditch using scopes at all and just stick with libraries.
Quote:
Originally Posted by Anitarf
I'd second Purge's suggestion of only using libraries, it's better to always explicitly write down the requirements of your code.
accidentaly already doing that, as it happens

thanks again for the info