0
\$\begingroup\$

haven't used this site before so I'm sorry if I format my post badly.

I am trying to build a stackable inventory for my game in c# XNA and the problem I've encountered is when an item in my inventory reaches the maximum stackable count (10) and creates a second instance of the item in the second slot of my inventory the second instance retains the count value of the first item.

So 10 items in my inventory have a value of 10 in the first slot, but 11 items will have a value of 10 in slot 1 and 10 in slot 2

4 pictures illustrating my problem: http://imgur.com/a/KZ5EL

class Backpack
{
         // 25 Inventory Slots
    public int itemMax = 25;
    public List<Items> ItemList = new List<Items>();
    int end = 1;

         //Funtction to add new items
    public void Add(Items newItem)
    {

        if (ItemList.Count <= itemMax)
        {

               //this if statement makes a list item add if the list.count is zero
            if (ItemList.Count < end)
            {
                ItemList.Add(newItem);
            }

            for (int x = 0; x < ItemList.Count; x++)
            {

                if (ItemList[x].nameID == newItem.nameID)
                {
                        //Checks for stackable item and less than 10 counts of item
                    if (ItemList[x].isStackable && ItemList[x].Count < 10)
                    {

                        ItemList[x].Count += 1;
                        break;
                    }
                    else
                    {
                        ItemList.Add(newItem);

                        end += 1;
                        break;
                    }
                }
                else
                {
                    ItemList.Add(newItem);
                    end += 1;
                }
            }

EDIT for floAr - I access the function like this:

        Items i;
        i = new Items();

        i.nameID = 1;
        i.Name = "Hog Meat";
        i.Item = ItemClass.Useable;
        i.Description = "Regenerates 30% hunger.";
        i.isStackable = true;
        i.Image = Content.Load<Texture2D>("hogmeat");
        i.sRect = new Rectangle(0, 0, 16, 16);

       backpack.Add(i);
\$\endgroup\$
  • \$\begingroup\$ Are you creating new items with the 'new' keyword. If you just keep passing the same item into the add item function you will actually work on the same object, as classes are passed by reference in C# \$\endgroup\$ – floAr Oct 12 '13 at 21:18
  • \$\begingroup\$ @floAr I don't know how to add code into a comment I will edit my post to show how I call the function. \$\endgroup\$ – David Antonijevic Oct 12 '13 at 21:58
0
\$\begingroup\$

I created a minimal test project to test your solution. The problem with the 2nd stack having a count of 10 is related to some reference and value passing mistakes. There are plenty of resources about this topic online (search for 'pass by value vs pass by reference c#')

However I discovered a new bug :) When the first stack is full, the backpack always created new stacks with count 1, as you don´t check if the second stack still has room:

else
{
    ItemList.Add(newItem);
    end += 1;
    break;
}

But here is my testing configuration (Console Application): Program.cs (Main class):

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Backpack b = new Backpack();
            for (int i = 0; i < 15; i++)
            {
                b.Add(new Items() { nameID = "myItem", isStackable = true, Count = 1 });
            }
            Console.Read();
        }
    }
}

Just creating a backpack an passing 15 NEW items to it. Backpack.cs is the one taken from here.

And a simple Items.cs stub, just with the necessary Properties:

namespace ConsoleApplication1
{
    class Items
    {
        public object nameID { get; set; }

        public bool isStackable { get; set; }

        public int Count { get; set; }
    }
}

If I run this and inspect the backpack after the for loop, it contains 1 stack with count 10 and 5 stacks with count 1.

Try to check if you are actually generating Items with the new keyword, this should fix your first error

Feel free to comment as soon as new questions arise!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.