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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Very basic Inventory script

Discussion in 'Scripting' started by timoshaddix, Jan 24, 2016.

  1. timoshaddix

    timoshaddix

    Joined:
    Jan 7, 2014
    Posts:
    64
    Hello,

    I am a beginner in programming with Unity!
    I do know the very basics, like arrays, lists, variables etc.

    But one thing i just can't create is an inventory,
    Can you give me soms basics of how to create an inventory.
    Im not asking for an complete script, because i really want to learn to write it myself.

    I'm quit sure i need to use arrays or lists, but i really have no clue.
    I've looked it up on the internet, but i couldn't find a decend tutorial, only for advanced programmers.

    I really hope someone can lead me in the right direction!

    greetings,
    Timo
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ok... lets start with "what inventory do you want to make?". What you want it to do will determine it's design.
     
  3. mickmarona

    mickmarona

    Joined:
    Apr 7, 2015
    Posts:
    44
    The most basic inventory script I can think of:

    Code (CSharp):
    1. //Declare your item types
    2. enum ItemTypes{Arrows, Potions, EtCetera}
    3. //Make an array to hold your count for each item
    4. int[] items = new int[howManyItemTypes]
    5. //Give the player an arrow
    6. ++items[(int)Arrows];
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    That's an excellent start, but take the thought a little bit further and perhaps write yourself an API for the inventory system.

    What calls would a an underlying game make to this inventory system?

    Let's say the class is InventorySystem. I could imagine it has these methods:

    void Initialize();
    void AddItem( ItemType itemType, int Count);
    void RemoveItem( ItemType itemType, int Count);
    int CheckItemCount( ItemType itemType, int Count);
    bool InventorySystem.CheckIfWeHaveItem( ItemType itemType, int Count);
    ItemType[] TellMeItemsThatIHaveAtLeastOneOf();

    In the future you might want to be able to save it to disk:

    void SaveToDisk( string fileName);
    void LoadFromDisk( string fileName);

    That should give you a lot of mental tools to begin digesting the problem on your own terms.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    dictionary would be better that that kind of inventory, it prevents the player having two slots for the same thing...
     
    orchard800 likes this.
  6. timoshaddix

    timoshaddix

    Joined:
    Jan 7, 2014
    Posts:
    64
    i want to create a simple inventory script where you can store like 3 random items in which you need to go to the next level, and like HP potions and mana potion!
     
  7. mickmarona

    mickmarona

    Joined:
    Apr 7, 2015
    Posts:
    44
    The enum already ensures that cannot happen, since each definition in the enum is linked with a number (e.g. Arrows = 0, and so always and only refers to slot 0, in turn only ever representing how many arrows you have)
     
  8. timoshaddix

    timoshaddix

    Joined:
    Jan 7, 2014
    Posts:
    64

    This is still a bit too complicated for me.
    Do i need to make an enum to create the ItemType?
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Inventories are complicated. You are essentially writing a database. Think of it that way.

    ItemTypes are just "nice" names for integers underneath it all. You could agree that "sword is 1, knife is 2, potion is 3," and never have to use enums at all, but you will probably eventually regret that decision. Just use enums appropriately, and if you're not sure what that means, rewind your programming learning a little bit and start with a simpler thing than an inventory system.
     
  10. timoshaddix

    timoshaddix

    Joined:
    Jan 7, 2014
    Posts:
    64
    alright, i'll rewind it a bit!
    i'm creating a low poly RPG with a friend of mine, i've created an enemy who follows you etc.
    But that's more of a unity system,

    What's a good thing to program where i can learn from.
    do you have any good practise for Enums?
    because i really want to learn it!
     
  11. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    He might as well try the ScriptableObject way of making inventories. But then we're beyond basic, making editor tools to create the database.
     
  12. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    This is using UGUI

    This is using GUI


    I had wacthed them before and it help me
     
  13. Ijaicdreox

    Ijaicdreox

    Joined:
    Jun 22, 2020
    Posts:
    3
    i am very confusd
     
  14. Ijaicdreox

    Ijaicdreox

    Joined:
    Jun 22, 2020
    Posts:
    3
    1. //Declare your item types
    2. enum ItemTypes{Arrows, Potions, EtCetera}
    3. //Make an array to hold your count for each item
    4. int[] items = new int[howManyItemTypes]
    5. //Give the player an arrow
    6. ++items[(int)Arrows]; is not working
     
  15. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Don't hijack a 4-year-old thread. Start your own. It's free.

    When you do, here is how:

    How to report problems productively in the Unity3D forums:

    http://plbm.com/?p=220
     
  16. Ijaicdreox

    Ijaicdreox

    Joined:
    Jun 22, 2020
    Posts:
    3