Item CanStack(Item item) always returns false.

I am trying to run an if statement where if an item can be stacked then it goes into the if statement, if it can not stack then it goes into the else statement.

The only thing is that whenever a stackable item comes into the if statement, CanStack always returns false.

Item randItem = ItemManager.CreateByName(listOfItems[rndm.Next(listOfItems.Length)]);

            if (randItem.CanStack(randItem))
            {
                entity.GiveItem(randItem);
                Server.Broadcast("Yup It Stacked.");
            }
            else
            {
                entity.GiveItem(randItem);
                Server.Broadcast(randItem.CanStack(randItem).ToString());
            }

 

It is because you are trying to check randItem with itself ( same object ).  CanStack need 2 different object.

Iq21dlQIlYnlQTl.jpg Lorenzo

It is because you are trying to check randItem with itself ( same object ).  CanStack need 2 different object.

Item randItem = ItemManager.CreateByName(listOfItems[rndm.Next(listOfItems.Length)]);

            if (item.CanStack(randItem) == true)
            {
                entity.GiveItem(randItem);
                Server.Broadcast("Yup It Stacked.");
            }
            else
            {
                entity.GiveItem(randItem);
                Server.Broadcast(randItem.CanStack(randItem).ToString());
            }

I am pulling the object "item" from the parameter that was passed to the method. I think this is what you are talking about when you say it needs two different objects. 

The boolean is still returning false.

Sample code where Item1 does not stack
but item2 and item3 does stack

        private void TestItem(BasePlayer player, string cmd, string[] args)
        {
            var item1 = ItemManager.CreateByName("wall.external.high.stone");
            if (item1.CanStack(item1)) Puts("Item1 can stack");
            else Puts("Item1 does not stack");

            var item2 = ItemManager.CreateByName("wall.external.high.stone");
            var item3 = ItemManager.CreateByName("wall.external.high.stone");
            if (item2.CanStack(item3)) Puts("Item2 can stack");
            else Puts("Item2 does not stack");

            item1.Remove();
            item2.Remove();
            item3.Remove();
            ItemManager.DoRemoves();
        }
Iq21dlQIlYnlQTl.jpg Lorenzo

Sample code where Item1 does not stack
but item2 and item3 does stack

        private void TestItem(BasePlayer player, string cmd, string[] args)
        {
            var item1 = ItemManager.CreateByName("wall.external.high.stone");
            if (item1.CanStack(item1)) Puts("Item1 can stack");
            else Puts("Item1 does not stack");

            var item2 = ItemManager.CreateByName("wall.external.high.stone");
            var item3 = ItemManager.CreateByName("wall.external.high.stone");
            if (item2.CanStack(item3)) Puts("Item2 can stack");
            else Puts("Item2 does not stack");

            item1.Remove();
            item2.Remove();
            item3.Remove();
            ItemManager.DoRemoves();
        }

In theory, this should work no?

            Item randItem1 = ItemManager.CreateByName(listOfItems[rndm.Next(listOfItems.Length)]);
            Item randItem2 = randItem1;

            if (randItem1.CanStack(randItem2))
            {
                entity.GiveItem(randItem1);
                Server.Broadcast("Yup It Stacked.");
            }
            else
            {
                entity.GiveItem(randItem1);
                Server.Broadcast(randItem1.CanStack(randItem2).ToString());
            }

In your example, randItem1 and randItem2 are the same object.
and ranItem1.canStack(randItem2) will be false.

if you test  (randItem1 ==randItem2)  , it will be true.