Hesitating between approaches for items/abilities/usables

Hi !

I’m planning to work on a survival-horror game, and I’m thinking about how I’ll be organizing the data. Survival-horror implies that the save system will be quite important, because of all the key items, the different states of the doors, etc.

At the moment, I’m scratching my head over items. When I think about it, it really comes down to the same issues that I encountered in a previous project, that was a card game : how/where to design the behaviour of some objects.

How I did it in my previous Card Game project :
In this previous project, every element had a Data part, that inherited from GameElementData : ScriptableObject, which held basic info such as ID, Name, Description, and Flavor Text. All my cards had a CardData : GameElementData.

I also had a plain C# class called Card, constructed with the CardData to initialize it. This class has a CardBehaviour property, which is an abstract class.

These cards were created by a Factory, which had a Dictionary<string, Type>. This means that depending on the ID, the Card’s CardBehaviour was set to an instance of the corresponding type.

Now, this has 2 aspects that feel hacky to me : this is prone to spelling error, and the dictionary has to be initialized “manually”.

Another approach that I used in the Card Game project :
In this same Card Game, the players could unlock items.

For this part, what I did was creating Monobehaviours, inheriting from an AbstractItem class and exposing an ItemData. I then created a GameObject for each of them, which were referenced in an ItemFactory as prototypes in a Dictionary<ItemData, AbstractItem>.

This way, when an item was requested, the Factory would Instantiate the corresponding one in the Dictionary.

This feels a bit cleaner, but I’m concerned about having a prototype of each item in the game as a GameObject. This seems really heavy for such a purpose, but had the advantage of being pretty straightforward and initialized at runtime.

My current question(s) :
Where should the behaviours of my objects go ?

For the inventory for example, I ultimately only need to keep track of which items does the player have, and in which quantity. So it could really boil down to Dictionary<ItemData, int> if we think about it.

But what if my player decides to use an item from the inventory : should I instantiate the behaviour at this very moment only ? Or should the inventory hold instances of an Item class, which has a Use() method ?

Should I try to use plain classes, and ultimately use Unity mostly as a view, or should I embrace the GameObject/MonoBehaviour and have actual instances of my items in the scene ?

I suppose that this applies to a lot of cases : abilities, weapons, cards, skills, items. Most of what I see online is very beginner-oriented, pure Scriptable-Object “architectures”, that do not scale well in my opinion.

My current takeaway from my previous experience is that I should use Scriptable Objects for data mostly (if not only), and put the behaviour somewhere else. I just struggle to decide what should my items be, and what would be part of a “decoration”.

Thanks a lot for reading me !

From reading this I saw a lot of the world ‘inherit’. And the generally the answer to the limitations of inheritance is to use composition instead. This can either be done through interfaces, or implementing some kind of component-like structure akin to Unity’s own components.

You can do this with plain C# objects with SerializeReference and custom inspector stuff. Can be as simple as:

public abstract class ItemComponentBase {}

[CreateAssetMenu]
public sealed class Item : ScriptableObject
{
    [SerializeReference]
    private List<ItemComponentBase> _itemComponents = new();
   
    public bool TryGetItemComponent<T>(out T component)
        where T : ItemComponentBase
    {
        component = null;
        int count = _itemComponents.Count;
       
        for (int i = 0; i < count; i++)
        {
            var c = _itemComponents[i];
            if (c is T cast)
            {
                component = cast;
                return true;
            }
        }
       
        return false;
    }
}

An then you only need the one item scriptable object class, and everything else is just components.

Well it should be on the items. That seems natural.

Thing about an inventory system is that a good one ends up being a model-view pattern. Maybe a ‘behaviour-model-view’. You have the behaviour of the items (scriptable objects). Then the model exists as pure data (plain C# data structures), and then you can have multiple kinds of views, depending on context. An inventory UI is one view, game objects in a scene is another view.

From start to end, in my current project it’s Item Scriptable Object → Plain C# Wrapper Item → Plain C# Inventory Data System → Various Kinds of Views. It’s a lot of code, but also proving to be very flexible and scalable.

Thank you for your reply !

I like the use of the SerializeReference to hold instances of a plain C# class in the Scriptable Object. I totally forgot about that.

What I tried was to have an IStrategy interface, whith MonoBehaviours that implement it, such has HealStrategy, CombineStrategy, etc.

My ItemController is a MonoBehaviour that implements IUsable. When calling Use(), its iterates through all the IStrategy components on the object and calls ExecuteStrategy().

I see two advantages to this approach over the SerializeReference one :

  • less time dealing with Inspector stuff, hence more time working on the actual logic
  • direct references to other objects in the scene. For HealStrategy, I can use a serialized private reference to HealthController for example. It feels to me that this is less likely to end with singletons, static members that shouldn’t be static, or over shortcuts.

But I feel like I’m not grasping the actual advantages of your method. Would you argue that the Behaviour in the Scriptable Object approach has benefits over the MonoBehaviours approach, despite what I mentionned ? If so, what would they be ?

Thanks !

The only part of my inventory system that involves monobehaviours is the parts of the item that actually need to be in the ‘physical’ game world. Basically, it’s visual representation (including rigidbody, colliders, etc, if required). Ergo, the ‘view’ part of the model-view pattern.

In my current project that boils down to literally a single, generic prefab that can be passed an item, from which it builds it’s physical display (it’s a 2d project so this is a lot easier than 3d). One monobehaviour and one prefab for every item in the game.

Item behaviour is not a part of that whatsoever. Being pure data/code, it is better placed in the pure-data world (which includes Scriptable Objects). Ergo, the model part of model-view.

You can just pass some kind of generic reference to the player and let individual implementations scrape off whatever references they care about. No singleton or manager or whatnot required. No manual inspector referencing required, in fact.

Having them specifically reference the player or some kind of manager/controller feels like a huuuuuge anti-pattern that will lead to future pain, rather than an advantage.

Managers should never even be manually placed in a scene, personally. How will that work across multiple scenes? My player character lives in their own scene; how am I meant to reference it? (Hint: you can’t) Instead, in any interaction I just pass through the player’s root game object, and GetComponent and the like does the rest of the work.

And tools like Odin Inspector exist to make inspector stuff 100x easier.

Again, an item’s behaviour is pure data. It should operate on a ‘tell, don’t ask’ basis. Just call it, pass through the player, and it can indifferently operate on what it’s given. If you have a game with not just a player, but also NPCs that can all use items, it doesn’t take much to make this work universally with any given entity, player, npc, wild animal or whatever.

Putting this in plain C# object and scriptable objects make this entirely pluggable too. Again, one monobehaviour/prefab for all items in my game! To make a new item, I just make a new scriptable object, fill it with data, and there you go, brand new item (in fact my system is such that I can make a new item by just combining other items, but that’s another thing entirely).

This also means you don’t need a game object to be able to view the information of an item, or to use it. How will an inventory work when everything is a game object? It’ll be painful, I’ll tell you what. Games like Subnautica did this, and it’s a buggy mess.

This isn’t even getting to the dilemma that is save games that you mentioned was going to be quite important. The more you throw into monobehaviours, the harder your save-system is to work. On the other hand, the more your game state is represented in plain C# data, the easier it is to work a save game. Hell, if done right, and your game state is all plain C# data, then save games are pretty much free to implement.

All of this is in essence, getting all your ducks in a row. Sometimes, to avoid a huge amount of pain further down the line, you need to bear with a little pain upfront. Some of this will depend in the scale of this ‘survival-horror’ game, and how extensively items and inventory are a part of it. If it’s not a huge part, then maybe not all of this is applicable or necessary. If it’s a major part, then probably worth trying out some of these concepts.

Anyway, this post is long enough for now.

Thank you so much for this detailed answer. I really like how you argue on the tradeoffs that I’m hestitating to make, it’s really convincing.

Never thought of it this way. It’s absolutely true.

I’m still struggling to find the right balance between embracing Unity’s GameObject/MonoBehaviour system, and a more “classical” software development architecture. But this is very helpful.

Thanks a lot !