Page 1 of 1

XP points [SOLVED]

Posted: Thu Dec 19, 2019 7:48 am
by mschoenhals
How do I tie my own experience points into Quest Machine? That is, I would like a quest to give the player some experience points. Can I use a simple trigger to add experience or does it require further coding? Here's my code for tracking player experience points:

Code: Select all

public class Experience : MonoBehaviour, ISaveable
    {
        [SerializeField] float experiencePoints = 0;

        public event Action onExperienceGained;

        public void GainExperience(float experience)
        {
            experiencePoints += experience;
            onExperienceGained();
        }

        public float GetPoints()
        {
            return experiencePoints;
        }

        public object CaptureState()
        {
            return experiencePoints;
        }

        public void RestoreState(object state)
        {
            experiencePoints = (float)state;
        }
    }
Thank you for any help or suggestions! :D

Re: XP points

Posted: Thu Dec 19, 2019 10:21 am
by Tony Li
Hi,

The cleanest way is to make a quest action. This will allow you to select it from the Actions list dropdown:

qmGiveXPAction.png
qmGiveXPAction.png (17.13 KiB) Viewed 454 times

To make a quest action, duplicate QuestActionTemplate.cs and customize it where the comments indicate. For example:

GiveXPQuestAction.cs

Code: Select all

using UnityEngine;
namespace PixelCrushers.QuestMachine
{
    public class GiveXPQuestAction : QuestAction
    {
        public float xp;

        public override void Execute()
        {
            base.Execute();
            FindObjectOfType<Experience>().GainExperience(xp);
        }

        public override string GetEditorName()
        {
            return "Give " + xp + " XP";
        }
    }
}

Re: XP points

Posted: Fri Dec 20, 2019 10:05 am
by mschoenhals
Great!
This worked really well. Thank you!

Re: XP points [SOLVED]

Posted: Fri Dec 20, 2019 1:02 pm
by Tony Li
Happy to help!