Arrays??

Hi,

Whats the best way to store this data:

ID
Title
Description
Map
Image

I have multiple levels and I want to store references to each of them. Is there a way to store all that data in an array? So each bit, ie. myArray[0] would contain the entire

ID
Title
Description
Map
Image

If so, how can I access each part?

Thanks

Ever heard of something called array of objects? :smile:

All what you have to do is create a class that define all the wanted attributes, then use that class to define an array of that class.

class MyData {
   var id;
   var title;
   var description;
   var map;
   var image;
}

var dataArray : MyData[];

Then you would access them normally like

dataArray[0].id = 1;
print(dataArray[1].title);

Hope this help.

Ive tried your example, it says index out of range, any ideas?

Arrays have size you know. So you have to create an array with the size you want, only then you can use the index that is between that range. My previous example only show how to define/use such array without a specific size as you can do that yourself.

Specifying a size is really easy

dataArray = new MyData[5];

Then you are allowed to use the items from 0 til 4 or use the .Push(item) and .Pop() function to add/remove to the array.

Ive tried that, still indexoutofrange.

Heres my code, any ideas:

class MyData { 
   var id; 
   var title; 
   var description; 
   var map; 
   var image; 
} 

var dataArray:MyData[] = new MyData[5];


dataArray[0].id = 1; 
print(dataArray[0].id);

Dude, check this code, it works 100%

class MyData {
   var id;
   var title;
   var description;
   var map;
   var image;
}

var dataArray:MyData[] = new MyData[5];

function Update () {
	dataArray[0].id = 1;
	print(dataArray[0].id);
}

The index out-of-range problem might be from somewhere else, as the code above is working for me with no errors.

Good luck

You should probably try to initialize your array inside the Start() function if you want to change any members inside it. The Update() function would change them once a frame, which can lead to many problems.

I have made this mistake on many occasions…

…this only allocates the memory, it doesn’t create any variables IN the array… This basically creates an array of 3 NULL objects and trying to access any NULL object or it’s supposed contents will obviously cause an error.

var foo : bar[] = new bar[3];

function Start()
{
     for (var f : bar in foo)
          f = new bar();
}

An even simpler way is to use the other array form and just say

var foo : Array = new Array();

function Start()
{
     for (x = 0; x < 10; x++)
          foo[x] = new bar();
}

…but this method has the drawback of having to define the value before every time you use it…

var b : bar = foo[3];
Debug.Log(b.name);

now I have a question for username85:
Did I read you correctly that you can Push and Pop on a [ ] array??? I was under the impression that [ ] arrays you declare the size in advance and if you want to add stuff (i.e. push…) you had to either create a new array and copy all the data to that so you don’t loose anything, or create a new array and then concat them together afterwards…

Can you actually push into an [ ] array? Will that increase the size of the array or just use the next available NULL in the array…

GargerathSunman: Yes you are right about not using it in Update. However, the purpose was to give a simple working example. Where and how he decide to populate/reference the array elements is all up to him.

MrDude: You are right about the Push and Pop, they are only available in the non-generic Array object. The built-in arrays are primitive types that have no such member methods. I must have mixed that with the Array as I interchange them while coding.
However, what you said about having to loop through the element of built-in arrays and creating new instance of the object is redundant and has no need (unless you need to run the construct with some parameters). If you don’t believe me try my example and see for your self. So it is not true that each element will be null, and it wont cause any errors.
Also I have to disagree with you on “Array” being better than built-in arrays. From convenience prospective, yes I can agree. However, using fixed built-in arrays is more efficient than using “Array”.

If you need to add/remove stuff from [ ] arrays, you can convert to a dynamic array, do your pushing and popping, then convert back:

var aa = [1, 2, 3];
var bb : Array = aa;
bb.Push(4);
aa = bb.ToBuiltin(int);

–Eric

Yeah… that’s what I figured. Nice to know I was right about SOMETHING tonight :stuck_out_tongue: