- Home /
What would be your approach to having random categories and each category having more elements?
I am creating a drinking game, with different question types, such as "normal questions", "rule", "chugging" and so on.
What I did is I created a list, for example:
List<int> listOfQuestions = new List<int> { 1, 1, 2, 3, 3, 1, 1, 1, 1, 1, 1, 1 };
where 1s would be normal questions and if 1 was selected, it would pull out a random question out of a string list containing questions specified for "normal questions", 2s would be rules and 3s would be chugging. Numbers are chosen randomly after each click on the screen and therefore a question resembling that category would appear. I was wondering if there is a better way to do this because as im getting close to finishing the game it feels like this method is too clunky?
Answer by MickyX · May 03 at 02:39 PM
@SwathedOrange I think I understand what you want. Here is what I would do
Create a new class of Question
public class Question
{
public string description;
public int type;
}
I would create a list of that Question class
public List<Question> questions = new List<Question>();
Populate that list however you want, personally I would use a CSV in resources so I could edit the CSV and the questions would be updated.
To select a random question I would write a function to return a random question based on the type needed. Using Linq to get a question (Note you'll need using System.Linq;)
public Question GetQuestion(int type)
{
List<Question> tempList = (from x in questions where x.type == type select x).ToList();
return tempList[Random.Range(0, tempList.Count)];
}
There are a number of benefits to using this, it moves the questions into a external file for easier management, it allows you to add new fields to a question without a clunky string. It also allows for language translation in the external file if you wanted to take the game to other countries.
As an example for expansion I added the ASKED flag.
public class Question
{
public string description;
public int type;
public bool asked = false;
}
Using the new asked bool you can return questions that haven't yet been asked
List<Question> tempList = (from x in questions where x.type == type && !x.asked select x).ToList();
Its worth noting that that could lead to a list without a selection question due to all questions could have been asked, so you will have to make sure you handle that situation
Something like this, when all questions have been asked I reset all questions of that type
public Question GetQuestion(int type)
{
List<Question> tempList = (from x in questions where x.type == type &&!x.asked select x).ToList();
if(tempList.Count == 0)
{
ResetAllQuestions(type);
tempList = (from x in questions where x.type == type && !x.asked select x).ToList();
}
Question selectedQuestion = tempList[Random.Range(0, tempList.Count)];
selectedQuestion.asked = true;
return selectedQuestion;
}
public void ResetAllQuestions(int type)
{
foreach (var question in (from x in questions where x.type== type select x).ToList())
{
question.asked = false;
}
}
Hope that helps? I haven't tested any of this but should work Thanks Mike
Your answer
Follow this Question
Related Questions
Random objects in coordinates specific 1 Answer
Polybrush prefab rotation increment 0 Answers
Random.Range is not changing? 1 Answer
Random Generation Algorithm Error 3 Answers
Instantiate Prefab at random times but keep 3 from spawning 2 Answers