how to make different types of scriptable objects?

Hello,

Im trying to add items to my game but im stuck with different types. For food items, i wanna set stats on how much it fills hunger and heals, but not for Armor or Tools. How do i do it? i don’t have much code, this is all:

using UnityEngine;

[CreateAssetMenu(fileName ="New Item", menuName = "Inventory/Item")]

public class DefineItem : ScriptableObject
{
    #region Name_Icon_Type
    new public string name = "";
    public Sprite inventoryIcon = null;
    public ItemType type;
    #endregion

    public void Use(){
        if(type==ItemType.Food){

        }
    }


}

public enum ItemType{
    Food,
    Armor,
    Tool
}

I was thinking perhaps nested ScriptableObjects, but i don’t even really know how to use them and i only have a vague and probably a wrong idea of them

These things (inventories, shop systems, character customization, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.

They contain elements of:

  • a database of items that you may possibly possess / equip
  • a database of the items that you actually possess / equip currently
  • perhaps another database of your “storage” area at home base?
  • persistence of this information to storage between game runs
  • presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
  • interaction with items in the inventory or on the character or in the home base storage area
  • interaction with the world to get items in and out
  • dependence on asset definition (images, etc.) for presentation

Just the design choices of an inventory system can have a lot of complicating confounding issues, such as:

  • can you have multiple items? Is there a limit?
  • if there is an item limit, what is it? Total count? Weight? Size? Something else?
  • are those items shown individually or do they stack?
  • are coins / gems stacked but other stuff isn’t stacked?
  • do items have detailed data shown (durability, rarity, damage, etc.)?
  • can users combine items to make new items? How? Limits? Results? Messages of success/failure?
  • can users substantially modify items with other things like spells, gems, sockets, etc.?
  • does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
  • etc.

Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

Breaking down a large problem such as inventory:

If you want to see most of the steps involved, make a “micro inventory” in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).

Everything you learn doing that “micro inventory” of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.

The “trick” is to start simple. Make it work then make it better. Right off the bat “DefineItem” is likely not a great name for a class but an ItemType and if(type=ItemType.Food) in the logic means (to me) that it is already too complex. Why not separate classes to start with. Make them do what they need to do and contain the properties they need. Then at some point you notice you can combine and/or subclass some of them and improve it.

1 Like

This:

new public string name = "";

Don’t do this. You really shouldn’t be hiding key Unity properties (I think you’ll run into serialisation issues here too). Just give it a proper name like “ItemName”.

Kurt’s right though, flexible item systems are hard. Enums won’t cut it. But when you think about it, Unity has the perfect example of flexibility: components. In the same way we can make various game objects that are assembled out of different components, you can make different items by assembling them out of various ‘item components’.

How to do this? There are many ways: inheritance (not recommend), interfaces, serialising a list of plain class ‘item components’ into each item (ergo, abstract base class with child classes via SerializeReference), or using multiple scriptable objects to assemble a larger more complete item.

I’ve done the last two styles with good success, but they do require plenty of boiler plate and, I guess, intermediate to advanced coding knowledge.

In your case, I’d look at learning how to use interfaces. They’re the most flexible and straight forward approach.

1 Like