- Home /
Question by
iKamoficial · Dec 11, 2019 at 06:50 PM ·
2d gameinventoryinventory systemstacking
Stack Problem(Please help!)
I have an inventory script on the player an an object with the itemdatabase. I create a label on each item with the stackamount of the item. I store the amount in each item and update it for each of them but when I have two items with the same id instead of creating a new one with 1 stackAmount it creates one that has an amount linked to the first item. Like if I change the stackAmount it changes for both of them.
InventoryScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
public int slotsX, slotsY;
public GUISkin skin;
public List<Item> inventory = new List<Item>();
public List<Item> slots = new List<Item>();
public bool showInventory;
private ItemDatabase database;
private bool showTooltip;
private string tooltip;
public bool usingItem;
int lastBlockUsed;
private bool draggingItem;
private Item draggedItem;
private int previousIndex;
float screenRatio;
Vector2 ScaleOnRatio = new Vector2(0.1f, 0.1f);
GameObject player;
void Start()
{
database = GameObject.FindGameObjectWithTag("ItemDatabase").GetComponent<ItemDatabase>();
player = GameObject.FindGameObjectWithTag("Player");
for (int i = 0; i < (slotsX * slotsY); i++)
{
slots.Add(new Item());
inventory.Add(new Item());
}
AddItem(0);
AddItem(0);
AddItem(0);
AddItem(0);
AddItem(1);
AddItem(1);
}
void Update()
{
screenRatio = Screen.width / Screen.height;
if (Input.GetKeyDown(KeyCode.E))
{
showInventory = !showInventory;
}
if (player.GetComponent<Player>().BlockPlaced == true)
{
showInventory = true;
player.GetComponent<Player>().BlockPlaced = false;
}
}
void OnGUI()
{
tooltip = "";
GUI.skin = skin;
if (usingItem && Input.GetKeyDown(KeyCode.E))
{
AddItem(lastBlockUsed);
GameObject.FindGameObjectWithTag("Player").GetComponent<Player>().PrefabBlock = null;
usingItem = false;
}
if (showInventory)
{
DrawInventory();
}
if (showTooltip && showInventory)
GUI.Box(new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 200, 200), tooltip, skin.GetStyle("Tooltip"));
if (draggingItem)
GUI.DrawTexture(new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 50, 50), draggedItem.itemIcon);
}
void DrawInventory()
{
Event e = Event.current;
int i = 0;
for (int y = 0; y < slotsX; y++)
{
for (int x = 0; x < slotsY; x++)
{
Rect slotRect = new Rect(x * 70, y * 70, 700 * ScaleOnRatio.x, 700 * (screenRatio * ScaleOnRatio.y));
GUI.Box(new Rect(slotRect), "", skin.GetStyle("Slot"));
slots[i] = inventory[i];
if (slots[i].itemName != null)
{
GUI.DrawTexture(slotRect, slots[i].itemIcon);
if(slots[i].itemAmount > 1)
GUI.Label(slotRect, "" + slots[i].itemAmount, skin.GetStyle("Stack"));
if (slotRect.Contains(e.mousePosition))
{
if (!draggingItem)
{
CreateTooltip(slots[i]);
showTooltip = true;
}
if (e.button == 0 && e.type == EventType.MouseDrag && !draggingItem)
{
draggingItem = true;
previousIndex = i;
draggedItem = slots[i];
inventory[i] = new Item();
}
if (e.type == EventType.MouseUp && draggingItem)
{
inventory[previousIndex] = inventory[i];
inventory[i] = draggedItem;
draggingItem = false;
draggedItem = null;
}
if (e.isMouse && e.type == EventType.MouseDown && e.button == 1)
{
if (slots[i].itemType == Item.ItemType.Block || slots[i].itemType == Item.ItemType.Trap)
{
UseItem(slots[i], i, true);
}
}
}
}
else
{
if (slotRect.Contains(e.mousePosition))
{
if(e.type == EventType.MouseUp && draggingItem)
{
inventory[previousIndex] = inventory[i];
inventory[i] = draggedItem;
draggingItem = false;
draggedItem = null;
}
}
}
if (tooltip == "")
{
showTooltip = false;
}
i++;
}
}
}
string CreateTooltip(Item item)
{
if (item.itemType == Item.ItemType.Trap)
tooltip = "<color=#b85035>" + item.itemName + "</color>";
else
tooltip = item.itemName;
return tooltip;
}
void UseItem(Item item, int slot, bool deleteItem)
{
usingItem = true;
lastBlockUsed = item.itemID;
GameObject.FindGameObjectWithTag("Player").GetComponent<Player>().PrefabBlock = Resources.Load<GameObject>("PrefabBlock/" + item.itemName);
if (deleteItem && item.itemAmount <= 1)
{
inventory[slot] = new Item();
}
else if (deleteItem && item.itemAmount > 1)
{
item.itemAmount -= 1;
}
if (player.GetComponent<Player>().BuildMode == true && player.GetComponent<Player>().BlockPlaced == false)
showInventory = false;
showTooltip = false;
}
void RemoveItem(int id)
{
for (int i = 0; i < inventory.Count; i++)
{
if(inventory[i].itemID == id)
{
inventory[i] = new Item();
break;
}
}
}
public void AddItem(int id)
{
for (int i = 0; i < inventory.Count; i++)
{
if (inventory[i].itemAmount == inventory[i].stackMax || inventory[i].itemName == null)
{
for (int j = 0; j < database.items.Count; j++)
{
if (database.items[j].itemID == id)
{
inventory[i] = database.items[j];
}
}
break;
}
if (ItemInInventory(inventory[i], id) && inventory[i].itemAmount < inventory[i].stackMax)
{
if (inventory[i].itemID == id)
{
slots[i].itemAmount += 1;
break;
}
}
}
}
bool InventoryContains(int id)
{
bool result = false;
for (int i = 0; i < inventory.Count; i++)
{
result = inventory[i].itemID == id;
if (result)
{
break;
}
}
return result;
}
bool ItemInInventory(Item item, int id)
{
for (int i = 0; i < inventory.Count; i++)
{
if (inventory[i].itemID == item.itemID)
return true;
}
return false;
}
}
Item Database:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemDatabase : MonoBehaviour
{
public List<Item> items = new List<Item>();
void Start()
{
items.Add(new Item("SpikeTrap", 0, "A spike that appears from the ground ", 1, 0, Item.ItemType.Trap, 16));
items.Add(new Item("WoodFloor", 1, "a ", 1, 0, Item.ItemType.Block, 16));
items.Add(new Item("WoodFloor", 1, "a ", 1, 0, Item.ItemType.Block, 16));
}
}
ItemCreateScript:
using System.Collections;
using UnityEngine;
[System.Serializable]
public class Item
{
public string itemName;
public int itemID;
public string itemDesc;
public Texture2D itemIcon;
public int itemAmount;
public int itemSpeed;
public ItemType itemType;
public int stackMax;
public enum ItemType
{
Weapon,
Trap,
Monster,
Block
}
public Item(string name, int id, string desc, int amount, int speed, ItemType type, int stackmax)
{
itemName = name;
itemID = id;
itemDesc = desc;
itemIcon = Resources.Load<Texture2D>("Icons/" + name + "Icon");
itemAmount = amount;
itemSpeed = speed;
itemType = type;
stackMax = stackmax;
}
public Item()
{
itemID = -1;
}
}
Comment