r/Unity3D•
Copun

Best way to implement an item database?

Question

The problem: I need to create many different types of items with different functionalities. They implement the same interface, but I don't want to create separate scripts for each one.

Thoughts: Each item has an if that points to a script database containing all item functionality. But since I can't create anonymous Coroutines, I would have to create unique coroutines for each item, worse than creating separate scripts for each item. Any ideas?

Posted Apr 15, 2017, 9:20 PM UTC

39
kdpkke•

I don't know if I really understand your problem, but have you ever looked at Scriptable Objects? Maybe is what you are searching for!

2
CopunOP•

Hmm I'll look into them. Thanks for the input!

1
kdpkke•

Np dude, if you see that fits your needs, i tell you from my (noob) experience that they Save your life! They are kind of tedious to handle the first time you try to use them in your code, but when you get used to them, you will always want to use them for your projects!

1
PolloMagnifico•

Exactly what problems are you running into?

1
CopunOP•

It's not really a problem it's more a matter of convenience and organization. I currently store prefabs of items in a list and use that as a database. With that, for each item to implement different actions, each prefab requires their own unique script(implementing the same interface) With a lot of items this gets tedious as I need to create multiple different classes. I'm hoping for a better approach at this.

1
PolloMagnifico•

Well, I mean every item needs a script to tell it what to do, but there's nothing to keep you from using the same script on multiple items with different variables.

In theory, using a Parent Class with an Override function and several generic child classes should shorten your work significantly. So you might have a SingleHealing class that you can attach to your single healing items, GroupHealing that you can attach to group healing items, and DamageItems that you can attach to your damage items.

Then when you're ready to activate them you can call any of the three by using gameObject.GetComponent<ParentClass>().

And if you have something that doesn't fit those generic logic classes, you can create a custom one that inhereits and utilizes the override function to do something custom.

1
CopunOP•

hmm good point. Sort of the best of both worlds there.

1
PolloMagnifico•

I built my item system for an RPG this way.

I created a class that didn't inherit from MonoBehavior (ItemStats), and a serializeable class (ItemDatabase) that contained a List<ItemStats>

That let me modify values in the inspector. Each ItemStats had all the items stats contained in it, then had an ItemLogic variable.

So ItemLogic contained some generic functions like

void GenericHealing(int strength, ItemStats thisItem){}

then when I created a child class that inherited from ItemLogic, I could throw in a function like ActivateLogic(). Any time I needed to use an item, it would simply call GetComponent<ItemLogic>().ActivateLogic();, which would look like this.

public void ActivateLogic(){
    ItemStats currentItem = this.GetComponent<ItemStats>();
    GenericHealing(currentItem.strength, currentItem)
}

So anytime I had a simple, single character heal, I could throw that script on there and have it handled. If I needed something unique, I would just call my override function and write something specific to the item. Saved me a bunch of time, once I learned what I was doing.

1
HypnoToad0Snek.. Snek... SNEEEEK•

Create a folder with your items (scriptable objects), and create a static dictionary out of them every time your game starts (with the key being their name/id).

Keep in mind that youll have to have 2 separate classes: one (derived from scriptable object) for your item definitions and another one for item instances (so you can for example have 2 apples in your inventory with one being an entire apple and the other being half eaten)

1