Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Creating a shop and backpack inventory systems

Discussion in 'Scripting' started by UltraMel01, May 16, 2024.

  1. UltraMel01

    UltraMel01

    Joined:
    Apr 17, 2024
    Posts:
    5
    Hey guys,
    I have been stuck on this problem for a while now and I feel like the solution may be simple but I am honestly not sure what it is. I am trying to create a game where you have a backpack, a chest and a shop. The backpack is always visible and you can toggle between the chest and shop using buttons. Each inventory system is on its own panel which has a grid layout group so that the slots are shown in a grid. The slots are all instances of a prefab for each system respectfully - backpackslot, shopslot and chestslot. The prefabs are a series of panels with one having a button and one to show the icon of the item. When a slot is selected, it must keep being selected until a new slot is selected. I have a scriptable object that holds information of the items - icon, amount and item name. When a slot is selected, how do I store the icon and then use that icon to get the amount and item name that correlates to that icon?
    I have a separate buy button on the shop panel that will check if the player has enough money to buy the item, and if they do, it will move into their backpack
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,598
    Sounds like pretty standard inventory. There are an abundance of tutorials online that show a number of different ways you can approach inventory systems, have you tried any of them? Not much reason to repeat what is already available online unless you have a specific question related to your implementation along with some example code.

    You could also just buy a solution from the asset store too, I sell one, for example, but there are many.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,371
    Agreed... this Is just a "how do I do inventory" question and there are millions of YouTube tutorials already out there, so absolutely no sense one of us attempting to retype all the details here, which will certainly be FAR more than just some scripts.

    OP, you want to get working on this IMMEDIATELY. You have a tall steep learning curve to surmount, and all of your questions are really only answerable in the context of YOUR game design. The inventory for Call Of Duty is fundamentally different from Escape From Tarkov, for one example.

    Here is my crib sheet for these things:


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

    The following applies to ALL types of code listed above, but for simplicity I will call it "inventory."

    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.

    If you contemplate late-delivery of content (product expansion packs, DLC, etc.), all of that has to be folded into the data source architecture from the beginning.

    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.
     
    UltraMel01 likes this.
  4. UltraMel01

    UltraMel01

    Joined:
    Apr 17, 2024
    Posts:
    5
    Thank you for the help, I have been trying to follow along with tutorials on Youtube but I cannot seem to find one that would help with the requirements of the project I need to work on. I have just been very sick for a good part of this year and this is for a school project so I am behind in a lot of aspects of coding in unity - which I plan to fully look into during my school break. I will continue to watch tutorials and will look at the links you have sent to better my understanding. There is no world outside of the inventory, it is simply just inventory systems that I need to work with. Thank you again!
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,371
    Make sure you're not wasting time here. This is my approach to tutorials:

    Two steps to tutorials:

    1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
    2. stop and understand each step what is going on, otherwise you're just wasting time.

    After you do a tutorial, go have lunch, come back and then do the entire piece of work by yourself, perhaps only glancing at the tutorial.

    Until you can do this, you haven't actually "done" the tutorial and if you move ahead, you are building upon sand.
     
    UltraMel01 likes this.
  6. UltraMel01

    UltraMel01

    Joined:
    Apr 17, 2024
    Posts:
    5
    Will do, thank you!
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,396
    To be honest I don't think there is a tutorial out there than can properly explain the intricacies of making an inventory system. From my experience a lot of the learning was through a bit of trial and error, learning C# in general (as in, not specific to Unity), and lots of iteration.

    Most important to know is to keep the actual data (the items, inventory contents, etc) separate from the presentation (namely the UI). The data can just all be plain C# objects, scriptable objects, etc, which can be given to a UI system to display. And in most cases, input via the UI can in turn manipulate the data. Though you'll find this separation of concerns is important in a lot of aspects of coding, though especially for an inventory/item system.

    Otherwise a lot of the problem solving will be specific to your project.
     
    mopthrow, Ryiah and Kurt-Dekker like this.