Pages

Sunday, February 13, 2011

Unity3d + C#: My early inventory concepts... Part 1

A few month ago I gave myself a little challenge to develop an inventory script right after I finished up with my project at that time... Took me three days to finish, and haven't have the time to comment on it yet. And for some reason, I'm really bad at commenting script, and I'm bad at memorizing stuff too, always forget about how I wrote and structuring my script in the first place... haha

Now I'm taking some time off to document/comment on the early inventory concept that I've scripted. Before going into the script, let's visualize what I'm about to do. So right now, I need to create an array of empty spaces at a specific size of... say 4 by 4, for storing items... like how a normal inventory is like.




For this, I'll need two arrays, one is for storing the items, and the other one is for storing the positions (Rect) of the cubes for when I show them on screen using GUI.Box, which is sorta optional, but I wanna make things simpler by finishing all the calculation stuff before using them...

When I first started thinking about the structure of the script, I've been wanted to make it customizable, in terms of the size of the inventory.... So I first started by creating the four main variables:
private Rect[,] inventCont;
private int[,] inventCont;
public int arrayCapacityColumn = 4;
public int arrayCapacityRow = 4;

The first one is a Rect variable (used to store GUI positioning and size information), 2 dimensional; while the second one is an integer variable, also 2 dimensional. And as you can see I used the [,] for each of the variables created, as a call for creating a multidimensional array. To know more about multidimensional arrays and the array class, you can visit here: http://bit.ly/gKk4vS.

For the third and forth variables, as the name suggested, is the variables for declaring the size of the array. When put it together in the Start() function:
void Start() {
  inventCont = new Rect[arrayCapacityColumn, arrayCapacityRow];
  inventItems = new int[arrayCapacityColumn, arrayCapacityRow];
}

Next, I'm going to place something inside the arrays I just created. Before I continue, I'm going to declare 3 more variables for the size and space of the item slots in the inventory:
public float slotWidth = 75;
public float slotHeight = 75;
public float slotSpaces = 20;

Then back to the Start function, I write these:
for (int c = 0; c < arrayCapacityColumn; c++) {
  for (int r = 0; r < arrayCapacityRow; r++) {
      inventCont[c,r] = new Rect(slotSpaces + ((slotWidth + slotSpaces) * c), slotSpaces + ((slotHeight + slotSpaces) * r), slotWidth, slotHeight);
      inventItems[c,r] = 0;
  }
}

For the inventCont, which was a Rect array, I'm inserted a number of  16 (4 x 4) Rect variables with information about the placements and positions of the boxes in there.


And another 16 of integer number "0" in the inventItems array. The number "0" in this context means empty, making it an empty item slot. I can change the value to other numbers while keeping track of what each number is about.

After that, in the OnGUI() function, I'll create buttons to represent the empty button slots while adding GUI boxes with some specialize icon to represent those items inside the inventory. To do that, I'll need a new GUIStyle variable, a GUIStyle array.

Back to the top, I insert the following line:
public GUIStyle[] inventItemIcons;

What I basically want to do with this is to place in a few PNG or PSD files of my item's icon and then use them when referencing what I had in my inventory...

Back to the OnGUI function:
void OnGUI {
  for (int c = 0; c < arrayCapacityColumn; c++) {
    for (int r = 0; r < arrayCapacityRow; r++) {
       if (GUI.Button(inventCont[c,r], "")) {
       }
    }
  }

  for (int c = 0; c < arrayCapacityColumn; c++) {
    for (int r = 0; r < arrayCapacityRow; r++) {
      if (inventItems[c,r] > 0) {
        GUI.Box(inventCont[c,r], "", inventItemIcons[inventItems[c,r]]);
      }
    }
  }
}

Before testing out the script, I'm gonna place some items randomly in the array. To do that, I go to the Start function and insert these lines inside:
inventItems[2,1] = 1;
inventItems[1,0] = 2;
inventItems[3, 2] = 2;
inventItems[3, 0] = 1;
inventItems[0, 3] = 2;

Part 1's final result (after inserting the item icons into the GUIStyle array):


to be continued...

1 comment:

Yuba said...

I have a full inventory working and shit, if someone needs the source code i'm open to share