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?
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.
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.