#How to approach first person inventory/ Item system

1 messages · Page 1 of 1 (latest)

native acorn
#

Well, I’m new to Unity and C# overall, and I’m trying to wrap my head around how to implement a first person inventory / item system.

As an example, Minecraft and CS2 have the kind of inventory system I’m aiming for.

I can pick an item up from the ground and store it in my inventory, as well as drop it back to the ground. I can also equip an item in my hand, which can then be used for whatever functionality it has.

What I’m struggling with is how to design items that share the same behavior but have different properties. For example, I could have a green flashlight that is stronger and has more battery, and a yellow flashlight that is weaker and has less battery. I could have both of them in my inventory at the same time, and maybe even two green flashlights, but they still need to be their own instances.

I also need to connect functionality with visuals. That means when I equip item X, it should play item X’s animation in the player’s hand, as well as its sounds and usage behavior.

I was thinking about using interfaces for item usage, but then I started wondering: what about items that can’t be used on their own, and instead need an interactable, like a door?

How would i store all of that, properties, behaviour, type etc?

torpid girder
# native acorn Well, I’m new to Unity and C# overall, and I’m trying to wrap my head around how...

You basically need a way to serialize item properties/item definition(for example an SO), a POCO to represent the item instance logically and an object/class representing the item in the game world(a prefab probably).
The poco item would have a reference to its item definition and handle instance properties, like durability. It would also be the object that would be kept(referenced) in the inventories.
The world object would handle its behavior when dropped or equipped(or you might want a 4th object representing the equipped state).
All 3(4) could be (possibly abstract) base classes and have subclasses for separate behaviors that are not expressable via data alone.

At least that's a pattern I've been using for a while for complex item/inventory system.

native acorn
# torpid girder You basically need a way to serialize item properties/item definition(for exampl...

ok im not quite sure on how would i make that.

So, i can have a SO that inherits generic data but then sets Specific properties, that would be the definition. Then i can have a instance, that is a Constructor so i can create instances of the object based on its definition?

And this instance when created would be stored in the inventory, so i would also have to create Equipped script to handle the behaviours?

so that would be 4 scripts for a single item?

cinder grail
#

I will usually have an Item class that will have a reference to an ItemData SO. Inventory would be a list of Items. The Item class could have a struct(s) for stat modifers. I would handle Item Logic through interfaces.

native acorn
cinder grail
torpid girder
native acorn
native acorn
#

i think i will stop trying to think what is the best way and just code it out the basics so i can feel what it works out, or i will be stuck forever trying to over engineer it lol

#

ty for the replies