- Home /
Need expert help fixing my inventory. c#
hey there,
So I'm trying to create an inventory system, I've gotten to the point now where icons drag and drop into the inventory, but due to the fact i wish to make it both X and Y, i have the issue where once an item goes in, all slots become that item..
Can someone look at this code and help me fix it please:
[CODE]
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerHUD : MonoBehaviour {
//ActionBar
public SpellDataBase[] ActionBar = new SpellDataBase[12];
public List<SpellDataBase> _SpellBook = new List<SpellDataBase>();
public ItemDataBase[] InventoryBars = new ItemDataBase[1];
public List<ItemDataBase> _ItemList = new List<ItemDataBase>();
private SpellDataBase DraggedSpell;
private ItemDataBase DraggedItems;
private ItemDataBase DraggedItems2;
private int xx;
private int yy;
void Start () {
CurrentScore = PlayerPrefs.GetInt("CurrentScore");
CharactersName = PlayerPrefs.GetString("CharactersName");
#region Spells
_SpellBook.Add(new SpellDataBase.FireBall(this));
_SpellBook.Add(new SpellDataBase.IceBall(this));
#endregion
// Divide...
#region Items
_ItemList.Add(new ItemDataBase.Wood(this));
_ItemList.Add(new ItemDataBase.Wheat(this));
#endregion
}
void OnGUI () {
//if(Triggerinventory){
#region Inventory
int Inventoryx =0;
int _Offset = 0;
foreach (ItemDataBase Inventory_Item in InventoryBars)
{
Texture2D icon = Resources.Load("emptyactionbutton") as Texture2D;
if(Inventory_Item != null)
icon = Inventory_Item.icon;
for(int xx = 0; xx < 5; xx++){
for(int yy = 0; yy < 5; yy ++){
Rect Inventory_icon_rect_Items = new Rect(Screen.width/2 + ( xx * 60), 50 + (yy * 60), 60, 60);
GUI.DrawTexture(Inventory_icon_rect_Items,icon);
_Offset += 54;
if (DraggedItems != null && Inventory_icon_rect_Items.Contains(Event.current.mousePosition) && !Input.GetButton("Fire1"))
{
InventoryBars[Inventoryx] = DraggedItems;
DraggedItems = null;
}
else if (DraggedItems == null && Inventory_icon_rect_Items.Contains(Event.current.mousePosition) && Input.GetButtonDown("Fire1"))
{
DraggedItems = Inventory_Item;
InventoryBars[Inventoryx] = null;
}
}
}
// Inventoryx++;
}
//}
#endregion
// _Dragged Items
#region Dragged Items
if (DraggedItems != null)
{
Rect rec = new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 0, 0);
//Rect rec = mouse_pointer;
rec.width = DraggedItems.icon.width;
rec.x -= DraggedItems.icon.width / 2;
rec.y -= DraggedItems.icon.height / 2;
rec.height = DraggedItems.icon.height;
GUI.DrawTexture(rec, DraggedItems.icon);
}
//Whenever we release the drag,"delete" the spell we are moving
if (!Input.GetButton("Fire1"))
DraggedItems = null;
#endregion
}
}
[/CODE]
As you can see it basically allows the user to drag items from the item script, which is this:
[CODE]
using UnityEngine;
using System.Collections;
public abstract class ItemDataBase {
private string ItemName;
private int Value;
private string ItemType; // This will be the type of item, this will range from, ( uncommon, common, good, rare, epic, heroic, legendary )
public Texture2D icon;
private PlayerHUD owner;
public abstract void ItemClicked();
#region Misc Items
public class Wood : ItemDataBase
{
public Wood(PlayerHUD owner)
{
this.ItemName = "Wood";
this.Value = 10;
this.ItemType = "Common";
this.owner = owner;
this.icon = Resources.Load("WoodIcon") as Texture2D;
}
public override void ItemClicked()
{
Debug.Log(ItemName +"Value: " + Value + "Item Type: " + ItemType + "Description: " + "Smells kinda woody...");
}
}
public class Wheat: ItemDataBase
{
public Wheat(PlayerHUD owner)
{
this.ItemName = "Wheat";
this.ItemType = "Common";
this.Value = 2;
this.owner = owner;
this.icon = Resources.Load("wheat") as Texture2D;
}
public override void ItemClicked()
{
Debug.Log("Some mighty fine wheat...");
}
}
#endregion
#region Weapons
#endregion
#region Armour
#endregion
#region Consumables
#endregion
}
[/CODE]
Anyways, it only works if i make inventory bars 1, which i can understand. This will make each box that item though, when i take out the for loops for xx and yy i just get a set of bars going in a straight line.. can someone tell me how i can alter this to make a loot window which can be usable to the draggeditems part etc.
Many thanks, Ice
What's the difference between InventoryBars and _ItemList? And why are you looping through the inventory slots while looping through the InventoryBars?
I don't follow your reasoning, is all - and a few comments or a pseudo-code interpretation from you would go a long way to figure out just what it is you want to do.
Even a screenshot of it in action would be great.
This is how I would do it:
private List<Item> Inventory;
private void Start()
{
Inventory = new List<Item>();
}
private void DrawInventoryItems()
{
Vector2 currentInventoryPosition = Vector2.zero;
foreach (item in Inventory)
{
DrawItem(item, currentInventoryPosition);
currentInventoryPosition.x++;
if (currentInventoryPosition.x > maxGridPlaces.x)
{
//If it should drop down a row...
currentInventoryPosition.y++
currentInventoryPosition.x = 0;
}
}
}
private void DrawItem(Item itemToDraw, Vector2 inventoryOffset)
{
// Draw item at inventoryPosition.x + gridsize.x * inventoryOffset.x
// And inventoryPosition.y + gridsize.y * inventoryOffset.y
}
Now it's a simple task to, when you drop an item in the inventoryRect, check if a slot is empty, then add it to the list. You can rearrange the inventory by rearranging the list, if you want. (Lists have a handy Sort()-function, as well.)
I kind of understand what you mean, can you possibly redo it in c#, I don't have a solid background in Java
$$anonymous$$ost of that is c#. You just need to set the Inventory to private (or public...) and set a return type to the function (i.e. private void DrawInventoryItems()).
Then call the function from OnGUI(). You also need to create the DrawItem-function yourself, but it should be fairly easy (you have already made it in your original code, pretty much.)
I updated the code a bit.
Let me know if you need any more help.
Answer by Jamora · Sep 03, 2013 at 06:29 PM
From what I gathered from studying your code, it seems you have only one slot in your inventory, which you draw multiple times: public ItemDataBase[] InventoryBars = new ItemDataBase[1];
. This seems to be the cause of the problem.
As a fix, I would suggest you create more inventory slots, then iterate through them like you do currently, only put inventoryx
within the for loops. I would try inside the second for loop for starters.
I've just tried to do that and I can't get it to work. Can you please post and example so i can have a look? $$anonymous$$any thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113508im_/https://answers.unity.com/themes/thub/images/avi.jpg)