Hi guys! This might be a very basic question but I want to be sure before I keep going.
I’m trying to store posible positions for multiple gameobjects. However , each gameobject has a diferent number of posible positions, for example:
gameobject 1, can have 3 diferent positions, while gameobject 2 can have up to 8 diferent positions, etc… I wanted to use a jagged array for this, but I read somewhere that its really bad on performance. Is it true? Should I use a rectangular array instead? This is for an iphone game I’m working on.
Thanks alot!
Jagged array is basically an array of arrays. This means that there are several arrays living in memory. Each as it’s own lifetime.
Because Unity script (through .Net) is a managed runtime, object destruction happens out of user control. So part of a jagged array can be release while another part stay alive longer. For this reason it is bad on performance (and also it introduces a level of indirection when accessing values).
On the other hand if you have to store arrays of different size then there is no other solution. Using a rectangular array will use to much memory (you will have to allocate arrays with the biggest length of all your desired arrays, resulting in waste of place).
Jagged arrays are not really bad on performance if they are kept small (that is to say if don’t have a lot of arrays in the first dimension). Since iOS devices are really limited in term of memory space, I recommend to use them.