Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Alec1998 · May 24, 2015 at 06:05 AM · c#inventoryenumitemsitem pickup

Using enums to design an inventory system?

I've been prototyping an idea for a game lately, which would include an RTS-esque base building component. I need to set up an inventory system that would keep track of various resources (water, food, building materials, etc.) as well as things like ammo counts, weapons, armor, etc. The issue is, I'm not entirely sure how to go about that, since I'm still relatively new to the coding side of things. That said, a lot of things I've read suggest using enumerators for something like this. Is that the best way to go about this? And if so, how would I go about implementing that? Enums are something I haven't really used a whole lot. Like I said, still mostly new to serious coding.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Lo0NuhtiK · May 24, 2015 at 06:09 AM

Saw your question in the mod queue and figured I'd throw together a quick example class with some commenting in it to try to help give you and whoever else stumbles across this thread an idea or two, and try to make sense of what's going on in the scripts as well. There's inventory questions and such all over the place, but this site is becoming such a pain in the @$$ to sift through to find what you're looking for that it's no wonder why the duplicates are steadily becoming more frequent over the past few years. ...anyway... hope this helps someone before it gets lost in the pile xD ... any questions about it that the comments don't help with, just ask and I or someone else will try to explain.

 using UnityEngine ;
 using System.Collections ;
 
 [System.Serializable]
 public class Inventory
 {
     [System.Serializable]
     //class we'll use for all [amount]'s of each of our inventory items that use them
     public class Amount
     {
         //the current amount of whatever that we have (eg: .45 ammo)
         public int current ;
         //the max amount we can carry
         public int max ;
 
         /// <summary>
         /// Adds [value] to [current] and clamps current between zero and [max]
         /// </summary>
         public void AddCurrent(int value)
         {
             current = Mathf.Clamp(current + value, 0, max) ;
         }
 
         //default constructor
         public Amount(){}
         //another constructor which sets [current] and [max]
         public Amount(int current, int max)
         {
             this.max = max ;
             this.AddCurrent(current) ;
         }
     }
 
 
     [System.Serializable]
     public class BuildingMaterials
     {
         //our building material types...
         public enum Types
         {
             Wood, Iron, SomethingElse, AnotherThing
         }
         //the type of building material this is
         public Types type ;
         //the amount of this
         public Amount amount ;
 
         //default constructor
         public BuildingMaterials(){}
         //...another constructor
         public BuildingMaterials(Types type, Amount amount)
         {
             this.type = type ;
             this.amount = amount ;
         }
     }
 
     [System.Serializable]
     //same concept as the BuildingMaterials class
     public class Consumables
     {
         public enum Types
         {
             Beer, Pizza,
         }
 
         public Types type ;
         public Amount amount ;
 
         //constructors-->
         public Consumables(){}
 
         public Consumables(Types type, Amount amount)
         {
             this.type = type ;
             this.amount = amount ;
         }
 
         public Consumables(Types type)
         {
             this.type = type ;
             this.amount = new Amount() ;
         }
     }
 
     //these two variables are what will "hold/contain" all of our values
 
     //an array of building materials
     public BuildingMaterials[] buildingMaterials ;
     //array of consumables...
     public Consumables[] consumables ;
 
     /// <summary>
     /// Adds [value] to [buildingMaterials] where buildingMaterial.type = [type]
     /// </summary>
     public void AddBuildingMaterial(BuildingMaterials.Types type, int value)
     {
         for(int i = 0 ; i < this.buildingMaterials.Length ; i++)
         {
             if(this.buildingMaterials[i].type != type) continue ;
 
             this.buildingMaterials[i].amount.AddCurrent(value) ;
             break ;
         }
     }
 
     /// <summary>
     /// Adds [value] to [consumables] where consumable.type = [type] ...
     /// </summary>
     public void AddConsumable(Consumables.Types type, int value)
     {
         for(int i = 0 ; i < this.consumables.Length ; i++)
         {
             if(this.consumables[i].type != type) continue ;
             
             this.consumables[i].amount.AddCurrent(value) ;
             break ;
         }
     }
 
     /// <summary>
     /// Initializes a new instance of the <see cref="Inventory"/> class.
     /// if(populate = true) then construct this class to have one of each BuildingMaterial.Types and
     /// ---one of each Consumable.Types.
     /// else just set buildingMaterials and consumables to be new arrays of zero-length.
     /// Note : since [populate] is an optional parameter, you can leave it blank when creating a [new Inventory()]
     /// </summary>
     public Inventory(bool populate = false)
     {
         if(!populate)
         {
             this.buildingMaterials = new BuildingMaterials[0] ;
             this.consumables = new Consumables[0] ;
             return ;
         }
 
         //get the names from our BuildingMaterials.Types Enum
         string[] names = System.Enum.GetNames(typeof(BuildingMaterials.Types)) ;
         //make this array have a spot for each type
         this.buildingMaterials = new BuildingMaterials[names.Length] ;
         //loop through for each type
         for(int i = 0 ; i < names.Length ; i++)
         {
             //set each item in the array's type as one of the types, and gave it an amount value as well
             this.buildingMaterials[i] = new BuildingMaterials((BuildingMaterials.Types) i, new Amount(0, 50)) ;            
         }
 
         //same as what we just did for BuildingMaterials.Types, but now doing it for our consumables-->
         names = System.Enum.GetNames(typeof(Consumables.Types)) ;
 
         this.consumables = new Consumables[names.Length] ;
 
         for(int i = 0 ; i < names.Length ; i++)
         {
             this.consumables[i] = new Consumables((Consumables.Types) i, new Amount(7, 24)) ;
         }
     }
 }

  
  

And here's even a test GUI script to help a bit more... just slap this on some object in your scene and push 'play' ...old GUI xD

 using UnityEngine;
 using System.Collections;
 
 public class Tester_CS : MonoBehaviour
 {
     //blank inventory just to show you how it works
     public Inventory inv_1 = new Inventory() ;
     //populated inventory ... we'll use this in the GUI example below.
     public Inventory inv_2 = new Inventory(true) ;
     
     Vector2 scrollPos ;
 
     void OnGUI()
     {
         scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.MinWidth(Screen.width), GUILayout.MinHeight(Screen.height)) ;
 
         //loop through our building materials array of inv_2
         for(int i = 0 ; i < inv_2.buildingMaterials.Length ; i++)
         {
             GUILayout.BeginHorizontal() ;
             //making a label with each materials type(name) and amount values
             GUILayout.Label(inv_2.buildingMaterials[i].type.ToString() + " : " + inv_2.buildingMaterials[i].amount.current + "/"
                             + inv_2.buildingMaterials[i].amount.max) ;
             //making a couple buttons to show increasing/decreasing amount values
             GUILayout.BeginVertical(GUILayout.MaxWidth(50f), GUILayout.MinWidth(50f)) ;
             if(GUILayout.Button("+"))//add value using the Add...() method 
                 inv_2.AddBuildingMaterial(inv_2.buildingMaterials[i].type, 1) ;
             if(GUILayout.Button("-")) //add value using the AddCurrent() method of the Amount class
                 inv_2.buildingMaterials[i].amount.AddCurrent(-1) ;
             GUILayout.EndVertical() ;
             GUILayout.EndHorizontal() ;
         }
 
         //doing the same as above, but with consumables now-->
         for(int i = 0 ; i < inv_2.consumables.Length ; i++)
         {
             GUILayout.BeginHorizontal() ;
             GUILayout.Label(inv_2.consumables[i].type.ToString() + " : " + inv_2.consumables[i].amount.current + "/"
                             + inv_2.consumables[i].amount.max) ;
             GUILayout.BeginVertical(GUILayout.MaxWidth(50f), GUILayout.MinWidth(50f)) ;
             if(GUILayout.Button("+"))
                 inv_2.consumables[i].amount.AddCurrent(1) ;
             if(GUILayout.Button("-"))
                 inv_2.AddConsumable(inv_2.consumables[i].type, -1) ;
             GUILayout.EndVertical() ;
             GUILayout.EndHorizontal() ;
         }
 
         GUILayout.EndScrollView() ;
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

check if list contains item with matching string property 2 Answers

Database error updating inventory 0 Answers

Decorator pattern for inventory item class 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges