Search Unity

Question Help with a inventory system c#

Discussion in 'Scripting' started by BL4CK0UT1906, Nov 23, 2022.

  1. BL4CK0UT1906

    BL4CK0UT1906

    Joined:
    Apr 15, 2022
    Posts:
    26
    Hello and sorry for my english.

    I'm creating a RPG and i will use a inventory system. I have watched a lot of youtube videos but none of them really clicked in my head, they all look overly complex. I want the simpliest system ever. Grab an item, store, and open a "bag" listing my itens when i press "i". I really just want to check if my logic is correct, and really wish someone to help me.

    I'm thinking about doing something like:
    -Castin raycast from the camera.
    -Create an empty array called "inventory".
    -Check if it collides with an object on a specific layer (let's say, inventoryItem)
    -If the raycast is colliding and the player presses "E", i will destroy the "inventoryItem" and add a reference to it (the name) to the array.
    ---Right now, the first part is already complete. The "inventory" is created,and now, the player only need an UI for it.
    - Create an UI image for the inventory and another ui image for the "iten icons"
    -Then, when the player presses "i", i will activate the ui image and instantiate an "ui image" for every item on the array.
    -Finally, when dragging the mouse over the "item image", i need to create an "hover" effect with "use" and "drop" options.

    It looks a little raw, i know, i just really want to know if my logic should work or if i'm missing something. I really think this looks the better way. I'm more focused on the first part now (raycasting, array). So, is my logic correct? Is this an okay-ish way to do an inventory system?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Start with tutorials. You'll soon see if anything is missing in your logic or approach.

    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:

    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.
     
    BL4CK0UT1906 likes this.
  3. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    699
    The 2 main ways to make an inventory UI are like a bag as in UO where you drop content, or a list (or grid) with a scroller. The first option is more stylish, the second more simple.
     
    BL4CK0UT1906 likes this.
  4. BL4CK0UT1906

    BL4CK0UT1906

    Joined:
    Apr 15, 2022
    Posts:
    26
    Thank you so much for your answer. I'm starting to do it right now, i will check the thread you sent me. Thank you!!!!!!!