QuestList GiveReward method bugfix

Hi,

Code shared on this video around 16min or so has some mistakes on the for loops. I’m sharing mine below.

AddToFirstEmptySlot method has 2 params. Second param is item count.
For loops in the original code has index starting from 0, so the item count also ends up 0. I just changed the loops to start from 1 to reward.number inclusive. All credit goes to the fellow classmate who shared method in the video.

Hope this helps some head scratching :smiley:

Thanks so much for the great course.

private void GiveReward(Quest quest)
        {
            foreach (var reward in quest.Rewards)
            {
                if (reward.item.IsStackable())
                {
                    var added = GetComponent<Inventory>().AddToFirstEmptySlot(reward.item, reward.number);

                    if (!added) GetComponent<ItemDropper>().DropItem(reward.item, reward.number);
                }
                else // if not stackable give or drop several units
                {
                    var given = 0;

                    // add all possible to empty slots
                    for (var i = 1; i <= reward.number; i++)
                    {
                        var added = GetComponent<Inventory>().AddToFirstEmptySlot(reward.item, i);
                        
                        if (!added) break;
                        
                        given++;
                    }

                    // if entire reward is given go to next reward.
                    if (given == reward.number) continue;

                    // if given less then in reward, drop the difference
                    for (var i = 1; i <= reward.number; i++)
                    {
                        GetComponent<ItemDropper>().DropItem(reward.item, i);
                    }
                }
            }
        }

Thank you for this.

Privacy & Terms