Good news! To provide faster support and grow the community, we'll be changing the forum to "Read Only" and turn to Discord instead for support.
Join here: https://discord.gg/zjNj5zZ

PercentageItemGenerator minValue doesn't work

AndTorch
Posts: 5
Joined: Mon Jul 03, 2017 2:08 pm

PercentageItemGenerator minValue doesn't work

Postby AndTorch » Tue Aug 01, 2017 8:45 pm

Hi DevDog,

I noticed that when I use loot generator, minValue that I specify in inspector completely ignored. So, if for example I set minValue=2 maxValue=5 it still possible, that it will return 0 and Lootable object won't have any loot.

I checked source code and noticed that in

Code: Select all

public InventoryItemBase[] Generate(int minAmount, int maxAmount, bool createInstances = false)
        {
            RandomizeSeed();

            var toReturn = new List<InventoryItemBase>(maxAmount);
            foreach (int i in Enumerable.Range(0, items.Count()).OrderBy(x => randomGen.Next()))
            {
                if (toReturn.Count >= maxAmount)
                    break;

                if (Random.value > items[i].chanceFactor)
                    continue;

                if (createInstances)
                {
                    var instanceItem = UnityEngine.Object.Instantiate<InventoryItemBase>(items[i].item);
                    instanceItem.currentStackSize = (uint)Random.Range(items[i].minStackSize, items[i].maxStackSize);
                    instanceItem.gameObject.SetActive(false);
                    toReturn.Add(instanceItem);
                }
                else
                {
                    toReturn.Add(items[i].item);
                }
            }

            result = toReturn.ToArray();
            return result;
        }

minAmount parameter is never used. But I can't figure out how to fix it to work as expected. Could you please take a look and suggest some solution? Thanks!

AndTorch
Posts: 5
Joined: Mon Jul 03, 2017 2:08 pm

Re: PercentageItemGenerator minValue doesn't work

Postby AndTorch » Tue Aug 01, 2017 8:54 pm

I think I found the solution. Not the most elegant one, but it works. I just put foreach loop inside

Code: Select all

while (toReturn.Count < minAmount)
so it runs several times until return list contains required amount of elements.

The final code looks like this:

Code: Select all

public InventoryItemBase[] Generate(int minAmount, int maxAmount, bool createInstances = false)
        {
            RandomizeSeed();

            var toReturn = new List<InventoryItemBase>(maxAmount);
         while (toReturn.Count < minAmount)
         {
            foreach (int i in Enumerable.Range(0, items.Count()).OrderBy(x => randomGen.Next()))
            {
               if (toReturn.Count >= maxAmount)
                  break;

               if (Random.value > items[i].chanceFactor)
                  continue;

               if (createInstances)
               {
                  var instanceItem = UnityEngine.Object.Instantiate<InventoryItemBase>(items[i].item);
                  instanceItem.currentStackSize = (uint)Random.Range(items[i].minStackSize, items[i].maxStackSize);
                  instanceItem.gameObject.SetActive(false);
                  toReturn.Add(instanceItem);
               }
               else
               {
                  toReturn.Add(items[i].item);
               }
            }
         }

            result = toReturn.ToArray();
            return result;
        }


Hope it will be helpful for those who have similar issue. Cheers!

AndTorch
Posts: 5
Joined: Mon Jul 03, 2017 2:08 pm

Re: PercentageItemGenerator minValue doesn't work

Postby AndTorch » Fri Aug 04, 2017 11:25 pm

I found some issues in my solution. If you set minValue greater than 1, then it might be possible the situation when after first pass ToReturn.Count is still less than minValue, so it goes for second/third round checking the same array of items. In fact it can add the same item several times, so you will have sever stacks of same item which is not good.
To solve that I modified script to simply remove already added items from items[] array.

It also possible hat you may set minValue greater than number of items to choose from. In this case the minValue will be reassigned to items.Count().

The final code:

Code: Select all

        public InventoryItemBase[] Generate(int minAmount, int maxAmount, bool createInstances = false)
        {
            RandomizeSeed();

         if (items.Count() < minAmount)
         {
            minAmount = items.Count();
         }

            var toReturn = new List<InventoryItemBase>(maxAmount);
         while (toReturn.Count < minAmount)
         {
            foreach (int i in Enumerable.Range(0, items.Count()).OrderBy(x => randomGen.Next()))
            {
               if (toReturn.Count >= maxAmount)
                  break;

               if (items[i] == null)
                  break;

               if (Random.value > items[i].chanceFactor)
                  continue;

               if (createInstances)
               {
                  var instanceItem = UnityEngine.Object.Instantiate<InventoryItemBase>(items[i].item);
                  instanceItem.currentStackSize = (uint)Random.Range(items[i].minStackSize, items[i].maxStackSize);
                  instanceItem.gameObject.SetActive(false);
                  toReturn.Add(instanceItem);
               }
               else
               {
                  toReturn.Add(items[i].item);
               }

               items[i] = null;
            }
         }

            result = toReturn.ToArray();
            return result;
        }

After all this changes now it seems to work as expected.

Shipright
Posts: 68
Joined: Tue Jul 12, 2016 7:24 am

Re: PercentageItemGenerator minValue doesn't work

Postby Shipright » Sat Aug 05, 2017 7:05 am

Thanks for sharing this!


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 1 guest