- Home /
Hi, I'm trying to make a list based quest manager but I'm struggling to extract certain variables from the list.
I'm trying the make the code switch to the next quest when you reach the required amount of points for each quest. For some reason it's not working and I really can'y work out why.
Any help would be great thanks!
Bit of script I'm trying to make work:
for (int i = currentPoints; i >= 0; i++)
{
if (Quests[i].pointsRequired <= currentPoints)
{
Debug.Log("ping");
currentQuestid += 1;
}
}
And the full script: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class questManager : MonoBehaviour
{
public int currentQuestid;
[System.Serializable]
public class Quest
{
public int pointsRequired;
public GameObject objects;
public int questID;
public string objective;
public string pointName;
public string info;
public float waitTime;
public bool enabled = false;
public Text objectiveText;
public Text remainingText;
public Text infoText;
}
public Quest[] Quests;
public int currentPoints;
public float currentdelay;
public int required;
void Start()
{
currentQuestid = 0;
currentPoints = 0;
for (int i = 0; i < Quests.Length; i++)
{
Quests[i].objectiveText.text = Quests[i].objective;
}
}
void Update()
{
for (int i = 0; i < Quests.Length; i++)
{
Quests[i].objects.SetActive(i == currentQuestid);
}
for (int i = 0; i < Quests.Length; i++)
{
Quests[i].remainingText.text = Quests[i].pointName + ": " + currentPoints + "/" + Quests[i].pointsRequired;
}
for (int i = currentPoints; i >= 0; i++)
{
if (Quests[i].pointsRequired <= currentPoints)
{
Debug.Log("ping");
currentQuestid += 1;
}
}
}
Comment
Answer by CmdrZin · Jan 27, 2021 at 11:13 PM
Something like this should work. I added a 'usedPoints' variable so that quest don't have accumulative points.
public int currentQuest = 0;
public int currentPoints = 0;
public int usedPoints = 0;
public bool IsQuestComplete(int questID)
{
bool results = false;
// Assuming that the Quests don't have higher and higher points required.
if( (currentPoints - usedPoints) >= quests[currentQuest].pointsRequired)
{
Debug.Log("Quest " + questID + " completed.");
usedPoints += quests[currentQuest].pointsRequired; // use up the points for this quest.
results = true;
}
return results;
}
// Call this when the current points change.
public bool AllQuestsCompleted()
{
bool results = false;
if(IsQuestComplete(currentQuest))
{
++currentQuest; // next quest.
if(currentQuest >= quests.Length)
{
Debug.Log("All quests completed.");
results = true;
}
}
return results;
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Missing objects upon List.Add(); 1 Answer
How to modify array values? 1 Answer
Best way to list all active quests in a GUI 1 Answer
How to make a simple gui list 0 Answers