- Home /
Inventory Item Check
Hi All,
I've got this inventory from someone else and its working perfect, but now i want to use it in a way that it checks if there are a certain amount of items in it, to open a door, for example a Key (but in some cases i want it to check if there are 4 or 5 items in the inventory. I've spend about 2 hours on it with a friend but we can't seem to get it right.
Inventory script:
var numberOfSlots=24;
var numberOfColumns=4;
var textureSize=64;
var showBag=true;
public var items : Item[];
private var windowRect: Rect;
function Start(){
var slots : float=numberOfSlots;
var cols : float=numberOfColumns;
items=new Item[numberOfSlots];
var rows=Mathf.Ceil(slots / cols);
var width = textureSize * numberOfColumns + 20;
var height = textureSize * rows + 30;
windowRect = Rect((Screen.width - width) / 2, (Screen.height - height) / 2, width, height);
}
function Update(){
if(Input.GetKeyDown("b")) showBag=!showBag;
//logic code here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
public function check(oName:String)
{
for(var i = 0; i < items.length; i++)
{
if(items[i])
{
if(items[i].name == oName)
{
return true;
}else{
return false;
}
}
}
}
function OnGUI(){
if(showBag){
GUI.depth = 0;
CreateWindow();
}
}
function DragInventory(windowID:int){
for(var i=0; i< numberOfSlots; i++){
var y=Mathf.Floor(i / numberOfColumns);
var x=i - y * numberOfColumns;
var rect = Rect(textureSize * x + 10, textureSize * y + 20, textureSize, textureSize);
var texture : Texture2D;
if(items[i])texture = items[i].texture;
var slot = GUI.Button(rect, texture);
if(rect.Contains(Event.current.mousePosition)){
if(items[i] == null && DragDrop.item != null && Input.GetMouseButtonUp(0)){
items[i] = DragDrop.item;
//if(items[i].gameObject){
//Destroy(items[i].gameObject);
//items[i].gameObject=null;
//}
}
if(Input.GetMouseButtonUp(1) && items[i] && DragDrop.toolSelected){
var go=new GameObject.CreatePrimitive(PrimitiveType.Cube);
go.renderer.material.mainTexture=items[i].texture;
go.AddComponent(Rigidbody);
go.AddComponent(AddToInventory);
var s=go.GetComponent(AddToInventory);
s.item=items[i];
items[i] = null;
DragDrop.toolSelected=false;
}
if(slot){}
}
}
GUI.DragWindow();
}
function AddItem(item : Item){
for(var i=0; i< numberOfSlots; i++){
if(!items[i]){
items[i]=item;
if(item.gameObject)Destroy(item.gameObject);
return;
}
}
}
function CreateWindow()
{
GUI.BeginGroup(windowRect);
windowRect = GUI.Window(0, windowRect, DragInventory, "Inventory");
GUI.EndGroup();
}
Can anyone help out? There are more scripts in this package, but for as far as my knowledge goes you dont need those, else ask me.
If you are willing to help me out do you want to work with sample items apple, banana, so i can recall everything perfectly.
Thanks in Advance.
Answer by Berenger · Mar 04, 2012 at 01:32 AM
You should use integers to identify and check the items, it's faster and safer.
Use prints to see what the code is doing, what is in the item array, if the comparison is done properly etc. I'd tell you to use the debugger and breakpoints, but it's making me crazy and I can't get it to work, so the print method is not that bad ^^
Thanks for the reply, and we was using the Debug.Log to see what piece of code did or did not work properly, but we cant find the right piece of code to actually get into the bag.
We have tried things like
function OnTriggerEnter () [ if (Bag.Items == ("Cube")); Debug.Log ("Cube Found")); ]
But those arent working, so you suggest we should make integers and work with true and false?
Personally I would stop using Arrays altogether, and swap over to Generic.List ins$$anonymous$$d. $$anonymous$$uch easier, and most of the functionality you need is just there as part of the class.
Hmm and that Generic.List is in the documentation? or do i have to google for that
It's in the .net reference, so you'll have to Google it. Remember that the Unity API isn't all you have available to you- you also have almost all of the .net framework to play with, as well!
Oke thanks, well im not really a programmer myself, 3D is my strongest point :P but ill search out how that works.