Getting XP as Reward

After all these lessons my head is spinning a little bit, but there is a function that i have to implement in my game. I wonder how the code has to look like, in order to also have the possability of getting XP as the reward and not only items for your inventory. I mean that simular to the reward experience that we get, when defeating an enemy. So we need to implement a new field to the scriptable object of the quest, to set the experience that we want to give as a reward. But then somehow we need to add this rewarded XP points to our our current points. I know this is probably super easy to do and only takes some line of code but atm I am super lost and dont want to mess up anything ^^ but it would be cool if somone could help me with that, or maybe somone already implemented that?

Awarding experience is easy…

GetComponent<Experience>().AddExperience(pointsToAward);

Am I right, that I have to add this line to the GiveReward function?

 private void GiveReward(Quest quest)
        {
            foreach (var reward in quest.GetRewards())
            {
               bool success = GetComponent<Inventory>().AddToFirstEmptySlot(reward.item, reward.number);
               if (!success)
               {
                    GetComponent<ItemDropper>().DropItem(reward.item, reward.number);
                   
                   GetComponent<Experience>().AddExperience(pointsToAward);
               } 
            }
        }

and where does the pointsToAward comes from? is that an extra SerializeField I have to put on top of QuestList to actually manage on how manny experience points he should get? Still a little bit confused on that one.

I would add a field/getter in the Quest itself.

There is another method for handling this, which is best explained in the next course, Shops and Abilities. There, we learn how to make a Coin Inventory Item, and more importanly how to automatically add the value of that coin to a Purse component. Here’s the rough idea, applied to experience:

First, you need an ExperienceAward Inventory Item

[CreateAssetMenu(fileName = "Experienc Award", menuName = "Rewards/Experience")]
public class ExperienceAward : InventoryItem
{
     [SerializeField] int experienceToAward;

     public int GetExperienceToAward() => experienceToAward;
}

Now you’ll drop add the created ExperienceToAward to the Quest’s rewards just like any InventoryItem.

Now for the magic: Create this interface

namespace GameDevTV.Inventories
{
    public interface IItemStore
    {
        int AddItems(InventoryItem item, int number);
    }
}

And in your Experience class, implement this interface

public class Experience : MonoBehaviour , IItemStore
        public int AddItems(InventoryItem item, int number)
        {
            if (item is ExperienceAward award)
            {
                GainExperience(award.GetExperienceToAward() * number); //Edited
                return number;
            }
            return 0;
        }  

Finally, you’ll need to make a change in Inventory to automagically have the experience added to our Experience component (in a very generic way through the interface)
Put this at the beginning of Inventory.AddToEmptySlot()

            foreach (var store in GetComponents<IItemStore>())
            {
                number -= store.AddItems(item, number);
            }
            if (number <= 0) return true;
            ///the rest of the method as normal

Now when you AddToFirstEmptySlot an Experience Award (which is just an InventoryItem you added to the quest rewards), Inventory will poll the character to see if any component can use the InventoryItem. Sam presents this as a way to drop coins and put the resulting coins in a Purse component.
More information can be found in the Shops module of the Shops and Abilities course.

Thank you so much for this Brian!

It almost works fine but the only thing that isnt working is actually the experienceToAward thing. I just tested it and picked up an Experience Item. Instead of adding for example 100XP he only adds 1XP to my Stats, no matter what I type in.

So i was asking myself if it has to do something with this line:

public int GetExperienceToAward() => experienceToAward;

Beside of that, it is working and he is actually adding XP to my Stats but only 1XP.

That was a mistake on my part. I left out part of the formula in AddItems()
Change the GainExperience line to:

GainExperience(award.GetExperienceToAward * number);

That worked perfectly fine now!

Thank you so much Brian!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms