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

[JASS] Quick Question About Structs

Status
Not open for further replies.
Level 4
Joined
Jun 8, 2007
Messages
89
As I have been learning vJass, I have become completely hooked on structs, heh. However, there is one thing I have seen lately that I really am unsure of what exactly it does. I first saw it in Vexorian's xe system, and have seen it around a few other places too. It looks like some sort of array index on a struct declaration, like so:

JASS:
struct SomeStruct[8190]
// struct data here ...
endstruct

I assume it's some kind of cap on the number of instances or something, but am unsure and can't seem to find any information after a while of searching. Also, is 8190 a special number of sorts (perhaps the highest valid value)? I have seen that specific number several times.

Thanks in advance!
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
It's kinda hard to explain. In order to explain it properly I will first explain how structs work internally...

When you declare a struct, each of its members gets turned into an array. For example:
JASS:
struct MyStruct
    integer i
    real r
    unit u
endstruct
Will end up as:
JASS:
globals
    integer array s__MyStruct_i
    real array s__MyStruct_r
    unit array s__MyStruct_u
endglobals

What allocate does, is it assigns an array position for the struct, then returns that array index in the form of an integer. Hence, you can pass structs to functions which take integers.

Create, unless you've made your own create method, does exactly the same thing as allocate. If you have made your own create method, however, as I did as an example in my previous post, you must use allocate to get your struct, before initializing it manually. Create only takes parameters if you tell it to.

That is what was posted on a thread of mine, concerning loads of questions about librarys, scopes, structs and methods,,
what .allocate() does (i think) is assing a new array to the struct(variables),,
 
Level 4
Joined
Jun 8, 2007
Messages
89
So, I'm not really sure what this means about the syntax in question, but if I took a guess it would have to be that the struct would be assigning the value returned by allocate() to a integer array with 8190 indexes?
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
Well, my guess is that it returns an unused integer (for the array), and when you call the struct, through unit/timer/item data, or whatever,, you get the array number and use it to index the variables,,

And Jass supports a maximum array of 8191,, which includes 0, so yes, 8190 is the maximum number of arrays,,
 
Level 9
Joined
Nov 28, 2008
Messages
704
Uhm.. has no one read the manual?

That denotes the maximum number of instances you want to have max basically You could put it to higher then that to have MORE then 8190 instances, just like vJASS allows you to do with other variables.

At least, thats what I thought.. and if that isnt how it works, then how Ive been doing things right and stil have em working with that syntax IDK.

Edit: To be absolutely clear, its in the manual, if yous et the number to say 180, it will only make a multiple of 8191 instances. EG ,if yous et it to 200, nothing stops you from accessing 800, and if you say 10000, you can do 13000, etc...
 
Level 4
Joined
Jun 8, 2007
Messages
89
Alright, thanks guys! I looked through the manual but didn't see it for some reason. I guess I'm just bad at searching... =/
 
Level 14
Joined
Nov 18, 2007
Messages
816
also not that arrays in structs MUST have a maximum size. Ensuring your code keeps to those limits is a thing you have to do yourself.

JASS:
struct Data[8191]
    private unit array UNITS[4]
endstruct

You can have up to 8191/4 instances of Data at the same time.
 
Level 4
Joined
Jun 8, 2007
Messages
89
Alright, so a quick clarification, if I have:

JASS:
struct SomeStruct[100]
// other stuff
endstruct

I can only do:

JASS:
local SomeStruct somestruct = SomeStruct.create()

100 times?

EDIT: This of course would assume that I never call somestruct.destroy().
 
Level 4
Joined
Jun 8, 2007
Messages
89
So, really it only works for increasing the cap above 8190, and the number is the total 'units' of data, so like one int and two doubles would be 8190/3 instances?
 
Last edited:
Level 40
Joined
Dec 14, 2005
Messages
10,532
No, it is divided by the largest array in the struct. If there are none, then it is just that number.

The default value is [8190].

Also, Deaod, you're slightly wrong - there are actually 8190 rather than 8191 indexes available in the first array, [1..8190]. 8191 is not used since it doesn't save in Save Game, and 0 is the "null" struct index. Subsequent arrays use [0..8190], though, so you end up 8191n-1 indexes rather than 8191n indexes for n arrays.

Also, I just checked and it does indeed set the cap to what you put it, so even though there are 8190 indexes available for the first array, if you set it to [20] it will only work for [1..20].
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
Yeah.

JASS:
struct someStruc
endstruct

Compiles to:

JASS:
globals
//JASSHelper struct globals:
constant integer si__someStruc=1
integer si__someStruc_F=0
integer si__someStruc_I=0
integer array si__someStruc_V

endglobals

//Generated allocator of someStruc
function s__someStruc__allocate takes nothing returns integer
 local integer this=si__someStruc_F
    if (this!=0) then
        set si__someStruc_F=si__someStruc_V[this]
    else
        set si__someStruc_I=si__someStruc_I+1
        set this=si__someStruc_I
    endif
    if (this>8190) then
        return 0
    endif

    set si__someStruc_V[this]=-1
 return this
endfunction

//Generated destructor of someStruc
function s__someStruc_destroy takes integer this returns nothing
    if this==null then
        return
    elseif (si__someStruc_V[this]!=-1) then
        return
    endif
    set si__someStruc_V[this]=si__someStruc_F
    set si__someStruc_F=this
endfunction
 
Level 4
Joined
Jun 8, 2007
Messages
89
Alright, sweet. That makes sense. However, just a quick thought. I assume that a static array inside of said struct wouldn't have any affect on the max instances, correct?

Edit: Fixed some wording.
 
Status
Not open for further replies.
Top