Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Help me with an Inventory system

Discussion in 'Scripting' started by zhopets, Aug 16, 2023.

  1. zhopets

    zhopets

    Joined:
    Aug 2, 2021
    Posts:
    3
    I am making an inventory system for my pet project and I have encountered a problem.

    The game is supposed to have a lot of different types of objects, such as weapons, shields, consumables, tools, magic items e.t.c. I made a scriptable object Item which is inherited by other classes such as Weapon.

    I want my player to be able to hold any type of item they desire and that those items will have unique effects depending on the type and variables of the item. If the player holds a weapon, they should be able to attack with it, if they hold a shield, they should be able to protect themselves.

    The problem is that I cant find a way to call class functions and variables effectively. I need a variable to hold an item in a hand, but if I make it type Item, I wont be able to call weapon-specific variables, but if I make it type Weapon, I wont be able to use that hand for anything but weapons.

    I am a novice programmer, so maybe I am missing some easy way to fix this, or maybe my decision to use scriptable objects and inheritance was faulty. Please let me know if there is a solution to my problem I cant see. Thank you in advance.
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    Most such cases can be generalized. All items would have, for example, an abstract Use() function. Weapons and Shields inherit from Item, and override the implementation with something Weapon- or Shield-specific. At the end of the day the player now simply "uses" whatever he has in that respective hand.

    Does anything speak against that in your case?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Welcome! This is not to discourage you, but just so you know what you're getting into and to help you plan for the amount of effort you are facing in this context...

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

    Inventory code never lives "all by itself." All inventory code is EXTREMELY tightly bound to prefabs and/or assets used to display and present and control the inventory. Problems and solutions must consider both code and assets as well as scene / prefab setup and connectivity.

    Inventories / shop systems / character selectors all 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 such a 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:

    https://forum.unity.com/threads/weapon-inventory-and-how-to-script-weapons.1046236/#post-6769558

    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.

    Breaking down large problems in general:

    https://forum.unity.com/threads/opt...n-an-asteroid-belt-game.1395319/#post-8781697

    The moment you put an inventory system into place is also a fantastic time to consider your data lifetime and persistence. Create a load/save game and put the inventory data store into that load/save data area and begin loading/saving the game state every time you run / stop the game. Doing this early in the development cycle will make things much easier later on.
     
  4. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I would research into "Script-to-Script communication". As there are many ways of doing it, find which way works best for your scenario. As far as grouping child classes, I personally use "Class Inheritance", but as you're probably noticing you can also do it with "Scriptable Objects", as again there are many ways.

    But once you master and fully understand communication between scripts, life will become much much easier. Or at least it was for me. :)