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
1
Question by stuart6854 · Dec 29, 2013 at 04:29 PM · c#inventoryitemadding

Adding Item to an Inventory.

I just can't seem to work out how to add Items to my Inventory when it already exists and the amount i'm adding go's over the 'maxStack' amount?

Code:

 List FilterItems(Item newitem){
 
         List<Item> result = new List<Item>();
 
         foreach(Item item in PlayerInventory.Inventory){//For 'Item' in Player's Inventory...
 
             if(item.ID == newitem.ID){//If newItem's ID = the loops current items ID... 
 
                 if((item.currentStack + newitem.currentStack) < item.maxStack){//If the current stack + newItems stack is less than the maxStack...
 
                     print ("Add");
 
                     result.Add(item);
 
                 } else{
 
                     print ("Do something here!");
 
                 }
 
             }
 
         }
 
         return result;
 
     }
 
  
 
 public void AddItem(Item newItem){
 
         if(playerInventory.inventory.count > 0){//If there are 1 or more stacks
 
             List<Item> stacks = FilterItems(newItem);
 
             if(stacks.Count > 0){//if there are more than 1 returned items
 
                 stacks[0].currentStack += newItem.currentStack;
 
             } else {//when there is no stack yet or all stacks are full
 
                 PlayerInventory.Inventory.Add(newItem);
 
             }
 
         } else{//Item is not stackable
 
             PlayerInventory.Inventory.Add(newItem);
 
         }
 
     }

So basically, say i have One stack of 7 wood and i want to add 5. How would I code it to fill up the current stack and put the remaining 2 wood into a new stack?

Please ask if you need anything else?

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
Best Answer

Answer by KellyThomas · Dec 29, 2013 at 05:14 PM

This would be one approach.

Points to consider:

  1. itemAmount, how much of the newItem we are trying to allocate

  2. availableSpace, how much room is available in any given item slot

  3. making sure we look after any remaining itemAmount once we have finished our search


    List FilterItems(Item newitem){ int itemAmount = newitem.currentStack; List result = new List(); foreach(Item item in PlayerInventory.Inventory){ if(item.ID == newitem.ID) { if(item.currentStack < item.maxStack){ int availableSpace = item.maxStack - item.currentStack; if (itemAmount < availableSpace) { item.currentStack += itemAmount; itemAmount = 0; } else { item.currentStack = item.maxStack; itemAmount -= availableSpace; } print ("Add"); result.Add(item); } } } if (itemAmount > 0) { newitem.currentStack = itemAmount; result.add(newitem); return result; }

Comment
Add comment · Show 4 · 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
avatar image stuart6854 · Dec 29, 2013 at 06:16 PM 0
Share

When i first add something, say the currentstack is 2 it will add 2 fine, but then if i add 3 the next time it will add 4?

avatar image KellyThomas · Dec 29, 2013 at 06:37 PM 1
Share

Step though it:

 first it takes the itemAmount
 then for each item with matching ID in the inventory
   if it has room:
     calculate the availableSpace
     if itemAmount is less than availableSpace:
       add all of itemAmount to currentStack
       set itemAmount to zero
     else:
       fill currentStack to capacity
       decrease itemAmount by the same value
     add this item to the results list
 if any itemAmount remains and it to the results list

I have tried to replicate the logic of your code and the request in the question. Some of the logic seems strange to me but I don't know the purpose of FilterItems() and how it fits into your game.

At the moment results will only contain items to which it has changed the currentStack value, but never removes any elements from the player inventory. Depending on your purpose that me mean that items are missing or duplicated.

It is more intended to show one way of solving the problem of handling "overflow" rather than to provide code you can paste into your project.

avatar image stuart6854 · Dec 29, 2013 at 10:13 PM 0
Share

Got it achieving what i want now. Thanks, using what you have said above i have gotten rid of the 'Filter' function and have managed to squeeze in modified code. :) Thanks very much!

avatar image Artifactx · Jan 17, 2015 at 05:53 PM 0
Share

Hey, here is a tutorial, that show you how to create an inventory, that has the most common operations: https://www.youtube.com/watch?v=$$anonymous$$LaGkc87dDQ

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My Ability Modification Script is adding "20" where it should be adding only "1" when using += 0 Answers

Distribute terrain in zones 3 Answers

Decorator pattern for inventory item class 0 Answers

Can't Assign Item In Array 1 Answer

Inventory AddItem help 1 Answer


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