How can you make an array in rows an columns?

Hey guys,

So I’m trying to make a inventory system, I’ve set up a very good way of doing an action bar and I want my inventory system to work on the same principles. I had assumed I could do this for a multidimensional array:

	public ItemDataBase[,,] ItemBar = new ItemDataBase[5,5];

Is there anyway i can turn this code so it calls on the item database but i can pass through a value of 5 for x and 5 for y, so when i do a for loop later the buttons will pass in both directions. For this to work I need this to happen from this line.

Many thanks

(If you need the script to get a better idea. I will have to PM you, it’s too long to put on this post.)

If I understand the question correctly, you want to dynamically size a 2D array, which is not too hard.

Using Nested Generic Lists

Using System.Collections.Generic;

...

public List<List<ItemDatabase > > ItemBar;

foo(int width, int height){
   ItemBar= new List< List<ItemDatabase>>();
   for(int i=0; i<height; i++){ //for every row
      ItemBar.Add( new List<ItemDatabase>() );//create a list
   }
}

bar(){
    //access an item in the list
    ItemBar[0][5];
}

The downside to this is that it can be a jagged array (that is, nothing forces each row to be the same length)

Using a MultiDimensional Array

public ItemDatabase[][] ItemBar;

foo(int width, int height){
   ItemBar= new ItemDatabase[height][];
   for(int i=0; i<height; i++){ //for every row
      ItemBar *= new ItemDatabase[width];//create an array*

}
}

bar(){
//access an item in the array
ItemBar[0][5];
}
Still a jagged array syntax, but when you set it up you force the size.
Alternative Array…(may not work?)

With all that said, I’m not entirely convinced that this won’t work.
public ItemDatabase[,] ItemBar;

foo(int width, int height){
ItemBar= new ItemDatabase[height,width];
}

bar(){
//access an item in the array
ItemBar[0,5];
}