inventory system help

so i’ve been working with arrays for a while now to create an inventory system, i even posted something today about it. i’m on and off working with them, trying something new, giving up, trying something new, etc. I decided to come here to look for some help.

I seem to be fine for the most part until it comes to adding new items and giving them unique attributes. for example, i usually have an additem() function with parameters such as name and description. when the player picks up another item, that item would broadcast to the player additem() with its individual parameters. that item would then be stored virtually as an element in an array belonging to the player. somehow, this never seems to work for whatever reason. i’ve come close to completely giving up and using a premade inventory script but i haven’t because i want to learn how to make one and i want the script to be my own.

so i’m not sure how to continue going about this. i’ve considered making the array full of invisible transforms with scripts that stored information. or maybe creating a class for each item. no idea what i’m doing wrong.

heres what i’ve been working on, it’s not complete with all the features i intended to add, but it should have the basics of what i’m trying to achieve. it gives nothing but an error (object reference not set to an instance of an object.)

private var inventory = new Array();



private var mouseLoc = new Array(); //mouse location

private var mouseClick = new Array(); //mouse click location



var testName : String;

var testDesc : String;

var testObj : GameObject;

var testType : int;

var testImg : Texture2D;



function Start(){ //mostly derived from other script.

	inventory = new Array(12);

	for(var i = 0;i < inventory.length;i++){

		inventory[i] = new Array(3); //make inventory an array. [0] stores the element, [1] stores the rect, [2] stores the hotkey.

	}

	for(var x = 0;x < inventory.length;x++){

		inventory[x][1] = Rect(203+(x*29),203, 24, 24); //Rect of the slot.

	}

	inventory[0][2] = KeyCode.Alpha1;

	inventory[1][2] = KeyCode.Alpha2;

	inventory[2][2] = KeyCode.Alpha3;

	inventory[3][2] = KeyCode.Alpha4;

	inventory[4][2] = KeyCode.Alpha5;

	inventory[5][2] = KeyCode.Alpha6;

	inventory[6][2] = KeyCode.Alpha7;

	inventory[7][2] = KeyCode.Alpha8;

	inventory[8][2] = KeyCode.Alpha9;

	inventory[9][2] = KeyCode.Alpha0;

	inventory[10][2] = KeyCode.Minus;

	inventory[11][2] = KeyCode.Equals; //

}



function Update (){

	if(Input.GetKeyDown("i")){ //Just a test.

		AddItem(testName,testDesc,testObj,testType,testImg); 

	}

}



function AddItem (n, d, o, t, i){ //Setting all the attributes of next element. name,desc,object,type,image.

	var nextSlot = findOpenSlot(inventory);

	inventory[nextSlot][0] = new ItemClass();

	inventory[nextSlot][0].name = n;

	inventory[nextSlot][0].description = d;

	inventory[nextSlot][0].object = o;

	inventory[nextSlot][0].type = t;

	inventory[nextSlot][0].image = i;

	GUI.DrawTexture(inventory[nextSlot][1],inventory[nextSlot][0].image); //draw the button with nextSlot's rect and image.

}



function findOpenSlot(arr){ //derived from another script

	var foundSlot = -1;

	for(var n = 0;n < arr.length;n++){

		if(!arr[n][0]){

			foundSlot = n;

			break;

		}

	}

	return foundSlot;

}





function checkHover(rect){

	var isHovering : boolean = false;

	if(Input.mousePosition.x > rect.xMin  Input.mousePosition.x < rect.xMax){

		if(Input.mousePosition.y > rect.yMin  Input.mousePosition.y < rect.yMax){ //If the mouse is within rect's boundaries, return as true.

			isHovering = true;

		}

	}

	return isHovering;

}

so please tell me what you think, am i doing it all wrong, should i try another method. i’m certainly learning a thing or two, but i can’t reach the end result i intend to achieve. this is all very frustrating, which i suppose is part of this type of work :stuck_out_tongue:

For one, I’d recommend an object-orient approach, something like

class Item {
public var initialized : boolean = false;
public var name : String;
...
}

Since most big-game inventories have a size limit, I don’t see any reason why you can’t use an allocated array, ie

var inventory : Item[20]

From there, use the initialized variable in each item to determine if that slot in the inventory is used up.
I know it’s a big revision, but this way should be much faster and less bug-prone.

I see what you mean an inventory full of objects that can have their variables changed to represent items. Thanks for the reply ill give it a shot

im afraid it didn’t work…unity thinks that the second line you gave me ends after Item. it just tells me to put a semicolon there. is that C++ you gave me?

var inventory : Item[20];

Only thing I can see wrong is I forgot to put a semicolon at the end of the line…

i see now! i wasn’t thinking of it as a builtin array. i recently got it up and running and now i’m well on my way to making a functional script. thanks for your help. here’s what i have, in case anyone’s interested:

var inventory = new ItemClass[3];

var length : int;



var testName : String;

var testImg : Texture2D;



var BaseIMG : Texture2D;

var BaseOBJ : GameObject;



function Start(){

    for(var x = 0;x < inventory.length;x++){ //Whatever elements are empty, fill those values with the empty values so we don't end up with an empty image.

        if(!inventory[x].initialized){    

            inventory[x].image = BaseIMG;

        }

    }

}



function AddItem(){ //add the item and initialize it so it isn't used.

    var openSlot = nextSlot(); // use the next slot to find the next empty item.

    inventory[openSlot].name = testName;

    inventory[openSlot].image = testImg;

    inventory[openSlot].initialized = true;

}    



function nextSlot(){

    var foundSlot = -1;

    for(var i = 0;i < inventory.length;i++){

        if(!inventory[i].initialized){//Find the first item not initialized

            foundSlot = i;

            break;

        }

    }

    return foundSlot;

}



function Update(){

    if(Input.GetKeyDown("k")){

        AddItem(); //Test the AddItem() function.

    }

}



function OnGUI(){

    for(var n = 0;n < inventory.length;n++){ //draw the textures for each item

        GUI.DrawTexture(Rect(10,10*(10*n),20,20),inventory[n].image);

    }

}

This makes 3 images appear along the left side of the screen. pressing k will change them to the testImg variable in order of whichever ones have not been changed yet.