- Home /
chest items get added to inventory
i've had this problem for some time with my chests
lets say i store 3 wood in the chest, close it and gather another 2, instead of having 2 in my inventory and 3 in the chest i have all 5 in my inventory
i followed a tutorial for both the inventory and chest system, but i changed the inventory system alot so i cant just redo the tutorial
i tried to fix this for some time, i think i found the thing that wrent wrong but i cant figure out how to fix it
heres all necessary code (unless im forgetting something) with notes added
https://hatebin.com/vlqyogayuy
heres what it looks like in action
Answer by Tom4nt · Jun 02, 2019 at 03:42 PM
So as understood, you have an inventory for your player and other for your chest. I think the best way of storing the items is using the Script in the chest that contains a List of items and other in the player, also with a list of items.
As you open the chest, you can access the List on the Chest object (via script) and transfer the items you want form it to the List on your player script. Then, when you close the chest, the items on it are independent but still there.
You can use
yourList1.Remove(item);
to remove at item from a list and then
yourList2.Add(item);
to add it to the other list.
but i have that already no?
isn't the problem with
public void AddItems()
{
if (chestItems != null)
{
foreach (Item item in chestItems)
{
item.$$anonymous$$ySlot.AddItem(item);
}
}
}
in chest because the script doesnt know that item.myslot refers to the chest and not in general? i dont know how to distinguish between chest and inventory slots
i tried this
public void AddItems()
{
if (chestItems != null)
{
foreach (Item item in chestItems)
{
if (item.$$anonymous$$ySlot.isChest)
{
item.$$anonymous$$ySlot.AddItem(item);
}
}
}
}
//In ChestBag
public void AddChestSlots(int slotCount)
{
for (int i = 0; i < slotCount; i++)
{
SlotScript slot = Instantiate(slotPrefab, transform).GetComponent<SlotScript>();
slot.$$anonymous$$yIndex = i;
slot.isChest = true;
$$anonymous$$ySlots.Add(slot);
}
}
but now this happens
As I see on your code, your item is referencing a slot in the UI, but I think you should do it on the other way around. If a slot references an item, you can easily set the item from the list for a specific slot in the player or chest inventory. I don't know what properties you have on your Item script but you can have a property to store the quantity of the item and display it in the slot. Also your item image is being stored in the slot, witch doesn't make total sense, as you should store it on the item itself.
Also, do you have a list of Items on your wooden source/pot? Because it is basically a chest at this point and it makes sense that you have one.
I'm sorry if I got something wrong. I'm a bit confused by your code. I would use a different approach to obtain this result.
Answer by hyosun0 · Jun 04, 2019 at 11:10 PM
thanks for helping @Tom4nt , but im not quite sure if i know what to do to change/fix it since im fairly new
maybe i should just skip the storage system for now and come back once i know more? although i feel like this shouldnt be too hard to fix, i just cant figure out how
the pot is weird, i tried to make it with stacks but it didnt really work so i had to make it like this
https://i.imgur.com/mL5hP0k.png
public class LootTable : MonoBehaviour
{
public Loot[] loot;
public List<Item> droppedItems = new List<Item>();
private Item tmp;
private bool rolled = false;
public bool opened = false;
private void Start()
{
droppedItems.Add(tmp);
}
public void AddLoot()
{
if (!rolled)
{
RollLoot();
}
droppedItems.Remove(tmp);
LootWindow.MyInstance.items.Clear();
LootWindow.MyInstance.items.AddRange(droppedItems);
LootWindow.MyInstance.itemCount = droppedItems.Count;
LootWindow.MyInstance.AddLoot();
}
public void RollLoot()
{
foreach (Loot item in loot)
{
int roll = Random.Range(0, 100);
if(roll <= item.MyDropChance)
{
droppedItems.Add(item.MyItem);
}
}
rolled = true;
}
}
my item class is this
[CreateAssetMenu]
public abstract class Item : ScriptableObject, IMoveable, IDescribeable
{
[HideInInspector]
public SlotScript slot;
public BagScript bag;
public Sprite itemSprite;
[SerializeField]
private string itemName;
[Range(1, 64)] public int maxStack = 64;
[SerializeField]
private Quality quality;
public Sprite MyIcon
{
get
{
return itemSprite;
}
}
public int MyMaxStack
{
get
{
return maxStack;
}
}
public BagScript MyBag
{
get
{
return bag;
}
set
{
bag = value;
}
}
public SlotScript MySlot
{
get
{
return slot;
}
set
{
slot = value;
}
}
public Quality MyQuality { get => quality;}
public string MyItemName { get => itemName;}
public virtual string GetDescription()
{
return string.Format("<color={0}>{1}</color>\n", QualityColor.MyColors[MyQuality], MyItemName);
}
public void Remove()
{
if(MySlot != null)
{
MySlot.RemoveItem(this);
}
}
}
with the items just inheriting from it (like wood (just fuel as of now) below)
[CreateAssetMenu(fileName = "Fuel", menuName = "Items/Fuel", order = 4)]
public class Fuel : Item
{
[SerializeField]
private bool isFuel;
[SerializeField]
private float fuel;
public float MyFuel
{
get
{
return fuel;
}
}
public bool IsFuel
{
get
{
return isFuel;
}
}
public override string GetDescription()
{
return base.GetDescription() + string.Format("\nFuel amount: {0}", MyFuel);
}
}
Your answer
Follow this Question
Related Questions
unity3d simple inventory 1 Answer
My script for pick up items work is wrong! Help please 1 Answer
Object Reference Not Set To An Instance of An Object Error 1 Answer
adding inventory items is half-working 0 Answers
Inventory AddItem help 1 Answer