- Home /
Inventory Management Issue
I have a GameObject you can walk on to pick it up. This GameObject has an ItemContainer class extending from MonoBehviour. ItemContainer has an Item property, which does NOT extend from MonoBehaviour.
When I pick this GameObject up I look within the ItemContainer and retrieve the Item within this class. I add this Item by copying it to my Inventory which has a list of items. Once I'm done, I delete this GameObject as we've picked it up already.
As expected I have various items I'd like to see in this game so when I make a Medkit class extending the Item things become problematic.
The Medkit class can override methods like DoOnUse which should apply healing effects to the player, however, my ItemContainer doesn't allow me to specify the type of Item in the Inspector. Eventually I can't configure my Items to be a child of Item in the ItemContainer's inspector.
If I got rid of the ItemContainer.cs and converted the Item class to extend from MonoBehaviour then adding the items to my inventory necessitates collecting them as GameObjects which is problematic if you have one hundred items of different types in your inventory.
If I used a Container for every child I have then that's too many scripts you have to do duplicates for and feels like a dirty solution.
If I wrote an Editor class extending ItemContainer's inspector then it's a lot of effort for something so simple and again there are so many steps just to make a simple Item.
I humbly ask for your help.
ItemContainer.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemContainer : MonoBehaviour
{
public Item item;
public void Awake()
{
SetGameObjectOfItem();
}
public virtual void SetGameObjectOfItem()
{
item.myGameObject = gameObject;
}
}
Item.cs
using UnityEngine;
[System.Serializable]
public class Item
{
[SerializeField]
public GameObject myGameObject;
[SerializeField]
public string uniqueID;
[SerializeField]
public string name;
[SerializeField]
public string pickUpText;
[SerializeField]
public bool isQuantifiable;
[SerializeField]
public int quantity;
public void IncrementQuantity()
{
quantity++;
}
public void DecrementQuantity()
{
quantity--;
}
[SerializeField]
public bool isEquippable;
public virtual void DoOnEquip(){}
public virtual void DoOnUnEquip(){}
public virtual void DoOnPickUp(){}
[SerializeField]
public bool isUsable;
public virtual void DoOnUse(){}
}
Medkit.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Medkit : Item
{
public override void DoOnUse()
{
base.DoOnUse();
//TODO: Insert healing code.
Debug.Log("Healing player!");
}
}
Answer by tuncOfGrayLake · Jun 30, 2017 at 01:22 PM
Since I wouldn't be having a great deal of variety of items in this game and the impact of performance wasn't as strong due to this fact I've made a decision bearing all this in mind. I modified my scripts to extend from MonoBehaviour and use an on scene GameObject that pools the collected items under itself. For duplicate Items I've devised a plan that uses a quantity modifier instead of pooling the same item twice or more.
Setting up any other solution seemed to go against the fundamental services Unity allowed me and solved my problem in the fastest manner.
Answer by ShadyProductions · Jun 29, 2017 at 04:57 PM
Add an itemtype enum orsomething on your item class that you can set?
This feels like a roundabout way of addressing this situation and it necessitates either duplicate declaration of an enum of all available item types or a code that uses reflection to look up all types and automatically generates this content. Then I need to write cases for what the code should do for each type and so forth.
You are already over complicating it anyway.. Ins$$anonymous$$d of giving the item to the item container, give it an int value of the item's id, so whenever you collide with your container, you can have a method that looks up the object that has the given id inside the container, that way you can retrieve a generic object or whatever you want to retrieve. Or you can make every possible object inherit from some interface then you can do something like
item : IDropable
medkit : IDropable
IDropable drop; //you can assign both item and medkit class
I think that's a good approach. I can simply store the ID in the ItemContainer and have a ScriptableObject of all available items and retrieve necessary info from thes places. The inventory of the player would simply have what the item ID is and the quantity for that item.
I had trouble understanding, however, what you meant by the example with IDroppable? Care to elaborate?
Edit: I was very happy with your suggestions and later on I tried another solution using Interfaces however it seemed to be again too much for too little. Just wanted to say thank you for your input. I appreciate it and glad you've shared your experience.
Your answer
Follow this Question
Related Questions
How do I retrieve an item from a list? 1 Answer
Managing Item Types 1 Answer
Item database with override functions 0 Answers