- Home /
Scan for a variable based upon an integer
so i am trying to make a quest system for a game and the problem currently at hand is that each one of the 60 quests has a save data variable. what i need to do is make it so the script checks a variable without running if statements 60 times.
if(questvar == 1){
status = Savedata.Quest1;
}
if(questvar == 2){
status = Savedata.Quest2;
}
if(questvar == 3){
status = Savedata.Quest3;
}
if(questvar == 4){
status = Savedata.Quest4;
}
//and continues all the way to 60
if there is already an answer out there please point me to it, if not any help on this would be appreciated -T.D.M.3
Answer by pako · Jun 22, 2015 at 09:20 AM
I think what your are trying to do can be simply accomplished as follows:
Each Quest script should have a public variable "Status", which is what you want to access, and has been assumed to be int in this example (but it can be anything you want). The "Status" variable replaces the QuestX variables.
All Quest scripts should be held (referenced) by the current script, meaning the one that has all the if statements.
Here's the code:
using System.Collections.Generic; //needed to use List
//Drag-n-Drop Quests in Inspector
//Or use GetComponent to populate the Quests List (Quests can then be made private)
public List<Quest> Quests; //hold all Quests in a List.
private int status;
//Call this method when you want to get the status
void CheckQuestStatus(int QuestNumber)
{
int questIndex = QuestNumber - 1; //First List index = 0
if (questIndex >= 0)
{
this.status = Quests[questIndex].Status;
}
else
{
Debug.Log("Quest Number must be greater than 0");
}
}
I forgot to mention that i work in java script so i don't Quite know how to convert this. aside from that, I think this will work. Thanks!
Answer by Xarbrough · Jun 21, 2015 at 11:20 PM
What are you exactly trying to do? I think you can greatly improve your design by getting rid of the integer "questvar".
In what form is your SaveData.QuestX?
Generally you should store all of your quests in either a Dictionary, so that you can retrieve a quest by its Dictionary key (a string name) or in a List, if they are consecutive. Then you could have a method "GetNextQuest" which just takes the current quest and returns the next item in the list.
i am trying to make a single script to display progress on any given Quest. There will be 60 instances of the Quest script, however i am just trying to make a lighter simpler script to compensate. the data is actually preloaded on start-up so i just need to access a variable on the load side of the save script.
i am also trying to make it so can have all 60 Quests available at once not just a series of tasks (more free-roam style)
Your answer
![](https://koobas.hobune.stream/wayback/20220613201239im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Hello can anyone help me i am new in scripting 1 Answer
How to make door open and close with Input.GetButtonDown? 1 Answer
How to check if variable defined in script is present in shader? 1 Answer
Importing a class into multiple scripts 0 Answers
how can I change a light with multiple triggers. ? 0 Answers