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