- Home /
Item attributes organization
I was wonder how to implement modular attributes on items, players, and anything else. As some of you guys know Minecraft, there are potions that store different attributes and affect the player when applied.
I want to know how those attributes are written. Are they made by writing a base script and writing multiple derived scripts for every unique attribute? Or is all of the attributes written in one script and, in the inspector, tell what attribute to apply?
This is a design issue and should probably go to the forums. UA is better for specific technical questions.
Answer by VesuvianPrime · Sep 10, 2014 at 10:34 PM
It's a very good question, and probably worth asking somewhere like StackOverflow rather than Unity Answers. Not to say Unity Answers is a bad place, just you're more likely to get programming experts weighing in on the problem: http://stackoverflow.com/questions/1338391/when-to-subclass-instead-of-differentiating-the-behaviour
This is how I separate my concerns:
Write interfaces to figure out what data, inputs, outputs and functionalities I want. I don't need to design my application to be interface driven, they're just nice as a template to work around.
Write an abstract base class that implements my base interface. This class is going to contain most of the functionality.
Write subclasses for the different interfaces. These are going to handle data for the most part.
My approach to Minecraft enchantments would be something like this (Haven't played Minecraft in a while, so I forget exactly what has what kinds of enchantment):
IEnchantment;
IEnchantmentWearable : IEnchantment;
IEnchantmentWeapon : IEnchantment;
IEnchantmentTool : IEnchantment;
IEnchantmentHelmet : IEnchantmentWearable; // I think helmet has it's own enchantments like water breathing, but also has armor and unbreaking bonuses
IEnchantmentBoots : IEnchantmentWearable; // Similarly, boots let you walk in lava?
// etc
Each of these interfaces would have a bunch of integer values representing the levels of each enchantment:
public int damageLevel;
public int unbreakingLevel;
public int waterBreathingLevel;
// etc
Finally I'd make my class heirarchy that implements the interfaces. Make a fancy base class that interacts with the Player class somehow. Make sure everthing is serializable and looks nice in Unity inspector.
Sorry for the crazy rambly post. It gets to a point where a lot of programming is gut instinct. Only you can decide when to suclass on a case by case basis.
Subclasses, like writing on new derived scripts for different attributes?
Correct. So you could do:
public abstract class Enchantment : IEnchantment
{
public string name;
// Contains logic for handling enchantment stats
}
public class EnchantmentWeapon : Enchantment, IEnchantmentWeapon
{
public int damageLevel;
}
// etc
And IEnchantment is the potions logic like throwing, splash range, and appliance to entities, right? Sorry for the slow response.
Answer by Cherno · Sep 11, 2014 at 12:27 AM
Just today I started using a XML database for my items. It's very handy once you get your head around it and from mmy limited experience, I can recommend it. You basically have all your items and their values inside a XML file and you read from it whenever you need to fill your Item class instance with values. You can also, like I did, just create a dictionary at the start of the game that has all items stored by name.
Here is a tutorial that will get you started:
Your answer

Follow this Question
Related Questions
Creating a custom List or Collection 1 Answer
Attaching stats to Gameobjects vs an Item database? 0 Answers
Inventory System: How to detect equal items on a list for increasing amount? 0 Answers
Can I refer to a custom abstract class as a MonoBehaviour property? 1 Answer
Self-assigning field in class 1 Answer