# How to calculate instances?

Discussion in 'Triggers & Scripts' started by Krogoth, Oct 17, 2012.

1. ### Krogoth

Joined:
Apr 5, 2011
Messages:
247
Resources:
0
Resources:
0
Code (vJASS):
struct X[10000]
integer a[2]
integer b[5]
endstruct

Vexorian writes that there are 2000 instances.
How?

### Code Moderator

Joined:
Nov 11, 2006
Messages:
7,427
Resources:
18
Icons:
1
Spells:
4
Tutorials:
9
JASS:
4
Resources:
18
10,000 / 5 = 2,000

The thing is that structs actually convert into global arrays. For example:
Code (vJASS):
struct A
real b
endstruct

Even though it is not listed as an array,
`real b`
is actually an array variable. When jasshelper converts vJASS to regular JASS, it does that to simulate struct instances. Whereas it seems that the variable is associated with a struct instance, it is actually just associated with an integer that acts as the index. However, you don't need to worry too much about understanding that at the moment. All you need to know is that struct members are eventually converted into arrays.

Therefore, when you declare an array variable as a struct member, it has to do some 2D interfacing. That's why if you have an integer array in a struct, you have to declare a size. The size limits how many total instances you can have for the struct. The general rule is that the variable with the highest array size will be the limiter. So:
Code (vJASS):
struct X
integer a[50]
integer b[32]
endstruct

The variable a will limit X to 8192/50 = ~163 instances.

Generally, try to keep the indices to a reasonable value. Don't make it 4192 and extend the struct to 40,000 instances just as insurance. Remember that jasshelper converts vJASS to regular JASS, so having extended struct instances and higher indexes will end up adding quite a bit of code.

3. ### Krogoth

Joined:
Apr 5, 2011
Messages:
247
Resources:
0
Resources:
0
:O

Thanks!

edit
This makes everything clear.
I just thought size should be total. =]