Announcements, support questions, and discussion for Quest Machine.
mschoenhals
Posts: 118 Joined: Thu Dec 19, 2019 7:38 am
Post
by mschoenhals » Thu Dec 19, 2019 7:48 am
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!
Last edited by
mschoenhals on Fri Dec 20, 2019 10:05 am, edited 1 time in total.
Tony Li
Posts: 22107 Joined: Thu Jul 18, 2013 1:27 pm
Post
by Tony Li » Thu Dec 19, 2019 10:21 am
Hi,
The cleanest way is to make a quest action. This will allow you to select it from the Actions list dropdown:
qmGiveXPAction.png (17.13 KiB) Viewed 455 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
Post
by mschoenhals » Fri Dec 20, 2019 10:05 am
Great!
This worked really well. Thank you!