- Home /
Decorator pattern for inventory item class
Thinking about having items as classes that hold a list of "attributes" like how a gameobject holds component That'd allow for varied items in terms of stats and other stuff, and less trouble regarding initializing/cloning items, also no issues with inheritance Thing is it could cause problem when say, an Item has the same attribute twice with different values Could be fixed by editor scripts but heh Does this sound like a correct implementation of items? Anything I should know?
Why would you need the same attribute twice? That makes no sense.
I mean, if it's just a list that holds the attribute, one could add one twice. I don't need it, I just don't know how to avoid it by design
But an 'Item' object can techniqually never have 2 same attributes. The easiest is to create a base class for your items with some basic attributes that each item could have. And then make seperate groups of classes for each type of item. Here's an example.
public abstract class ItemBase : $$anonymous$$onoBehaviour { }
public class Weapon : ItemBase {
}
public class Consumable : ItemBase {
}
public class $$anonymous$$anager : $$anonymous$$onoBehaviour {
public ItemBase[] Items; // can contain weapons, consumables etc
}
I'm not sure what you're asking here. The decorator pattern does not prevent duplicate decorators either. The decorator pattern is actually rarely used. In most cases you would have a strategy pattern (comparable with components and gameobjects). Is there a reason why an item can't be a gameobject / prefab? What issues do you mean when it comes to cloning? And what issues regarding to inheritance? A decorator creates a chain of classes where the decorator instance replaces the actual instance and wraps it's functionality. $$anonymous$$ost Linq implementations are decorators or adapters.
How and where do you actually want to define your different items? The creation of items is usually an edit time process unless you want random generated items.
You use the terms "item" and "attribute" in a very abstract way. It could mean almost anything. You should think about your question again and clear up what you actually want to know. Also you may want to get more concrete about your items and attributes. Is an item just the logical representation of an item? Or does it include any visual representation (2d / 3d)?
Basically, I want to be able to either generate an item out of a scriptableobject template or let a script generate its own items itself I don't want to have specific classes for different types of items and ins$$anonymous$$d add different types of stats/other thing to a base item (stuff like perks, different types of stats, etc), which an item would hold in a list
$$anonymous$$onobehaviours on gameobjects could work but supposedly lots of gameobjects lead to bad performance and also it could be troubles when it comes to saving them or transfering them between scenes
Your answer
Follow this Question
Related Questions
Inventory AddItem help 1 Answer
How i can make a script-made button interactable? 0 Answers
Can't Assign Item In Array 1 Answer