- Home /
Working with Items and Class Inheritance for an RPG Item Database
So I've been coming up with a plan on making an item database, and that is to make it a List of type Item.
public List<Item> items = new List<Item>();
I want to place all of my items, whether it is a weapon, a consumable, or a quest item onto a single database. I am using a serializable Item class to hold the common data, and then I have a Weapon, Consumable, and Quest class (which are all inherited to Item) so that they have their own individual variables.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
[System.Serializable]
public class Item
{
public int itemID;
public string itemName;
public string itemDesc;
public Image itemImage;
public enum ItemType
{
None,
Consumable,
Quest
}
public ItemType itemType;
}
And then a sample for the Consumable class would be
using UnityEngine;
using System.Collections;
public class ItemConsumable : Item
{
public int addHP;
public int addMP;
}
With the Item class serialized, I can edit each object in the database list via the inspector, but I am only able to edit variables in the Item class, and not on the child classes.
I was thinking of placing an enum to determine if the item is a weapon, a consumable, or etc,. Pop up an if statement and make Item as someItemType
or whatever. This is probably wrong and I've done my research on attempting that.
Is there any other way for this?
NOTE: I will not be using XML.
You could try using a scriptableobject ins$$anonymous$$d. Here is a forum thread that will get you started if you choose to do it this way, and the tutorial referred to in the thread.
Answer by phil_me_up · Feb 16, 2016 at 01:56 PM
As you've seen, there are limitations to what can be achieved simply by making the class serialisable. However, thankfully we can write our own inspectors for these things so you can totally customise not only what you can and can't see in the inspector, but also how they appear and how you can interact with them.
Do some reading up on: http://docs.unity3d.com/ScriptReference/Editor.OnInspectorGUI.html as a starting point.
Your answer
Follow this Question
Related Questions
Visual Studio Code Mac (1.29.1) problem with class inheritance Unity (2018.2.17f1) 0 Answers
MongoDB C# authentication exception 0 Answers
Inventory with Upgradable Items and Varying Stats 2 Answers
Class inheritance for Item system question: Instance needs to be both Weapon and Ammo subclass 2 Answers
get_version can only be called from the main thread. 0 Answers