XP points [SOLVED]

Announcements, support questions, and discussion for Quest Machine.
Post Reply
mschoenhals
Posts: 118
Joined: Thu Dec 19, 2019 7:38 am

XP points [SOLVED]

Post 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
Last edited by mschoenhals on Fri Dec 20, 2019 10:05 am, edited 1 time in total.
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: XP points

Post 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 450 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";
        }
    }
}
mschoenhals
Posts: 118
Joined: Thu Dec 19, 2019 7:38 am

Re: XP points

Post by mschoenhals »

Great!
This worked really well. Thank you!
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: XP points [SOLVED]

Post by Tony Li »

Happy to help!
Post Reply