- Home /
Managing Item Types
I have an Inventory System, where only certain types of Items should be able to be filled into predetermined Slots. For the first basic Prototype I was using an ItemType : Scriptable Object, of which an item can have multiple of, for example "weapon, usable, equip" or "backpack". I've been thinking about actually implementing the feature now, and I like the flexibility of being able to create a new ItemType in such a fast way, but the problem I'm facing, is that I would like to implement some additional functionality to certain types of Items, for example "consumables" being able to have a "consume" function. A solution would be to inherit from the base ItemData class and provide the function in a new "ConsumableItem" Class. This doesn't allow for Items to be able to have multiple "tags" anymore though, if more than one provides functions. I thought about using Interfaces in some way, but this wouldn't help me, if I wanted a single Instantiated Consumable to implement an Interface all of the other consumables don't need.
I like to keep the code very adaptable and futureproof. I've been watching a few gdc talks and videos on the topic and liked the functionality the Scriptable Objects provide me in this scenario. Do any of you have any advice on this? This is pretty much my first bigger scale project and the first time I've been using Scriptable Objects in this way, so please go easy on me.
I really dont understand how Interfaces are not the solution to your problem.
Just saw this. Yep they absolutely are. They exist exactly for this reason.
If they want implementations on the interface (to reduce unnecessary duplication) and are not using a recent version of c# they could either use interface extensions or an abstract class (which is still program$$anonymous$$g to an interface)
I thought about them in the wrong way. After thinking over all of it again, I noticed I was just being stupid.
I created Interfaces for each subtype that needed aaded functionality, like weapons, usables or equipment and implemented them in a weapon, usables, ecc class.
If I then need Items that need multiple of these I just create a new compund type that also inherits from base item and implements the interfaces needed and basically just has two single type objects saved inside of it.
For example
If I need a compond class for a weapon that is also a usable, then it inherits from itemdata and implements the interfaces IamaWeapon and IamUsable.
It basically only has two variables that consist of a weapon data object and a usable data object. It doesn't really have any variables of its own, and the functions implemented by the interfaces just access the functions inside the relevant data object saved in the compound class.
public class CompoundWeaponUsable : ItemData, IamAWeapon, IamUsable{
WeaponData weapondata;
UsableData usabledata;
public void example_weapon_interface_function () {
weapondata.example_weapon_interface_function ();
}
}
public class WeaponData : ItemData, IamAWeapon {
//weapon relevant data
}
public class UsableData : ItemData, IamUsable {
//usable relevant data
}
This does mean that ItemData data exists 3 times in this class, since all of the classes inherit from it, which is kinda bad, but if I were to change it by making ItemData into an interface, it'd lead to a lot of code replication, since each itemtype would need to implement that data and making a change to its structure would mean rewriting all of the classes that implement the interface. Also, this way, if I were to change some calculation in the way weapons work, for example, I wouldn't need to change the weaponData class AND all of the compound classes that implement its interface. Since at the moment the compound classes just access the logic that has been scripted in the "single type" classes. I hope my explanation of my solution was somewhat comprehensive
Answer by sacredgeometry · Jan 10, 2021 at 05:40 PM
Create an interface called something like ICarryable
and then put all of the members that pertain to being able to carry it on that interface and make your other item types implement that interface.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Inventory Management Issue 2 Answers
Item database with override functions 0 Answers
[C#]Inventory script help. 3 Answers
GetItemByID error 0 Answers