Search Unity

Trivia categories...

Discussion in 'Scripting' started by BillTheThrill, Oct 10, 2017.

  1. BillTheThrill

    BillTheThrill

    Joined:
    May 18, 2016
    Posts:
    18
    I hope this isn't a stupid question.

    So I followed along with the Unity tutorial for the "Quiz Game" and "Quiz Game 2" and whatnot, but what I want to know is how I would go about separating questions into categories.

    For now, I went with the idea of creating a CategoryData C# file and separating them into categories that way. I've done two example categories for just "Sports" and "Math" right now.

    And then I'm guessing that in the GameController script I would change

    questionPool = currentRoundData.questions;

    to

    questionPool = currentRoundData.categories.questions;

    However, how do I get it to know which category was chosen by the player? How do I link each specific category to a button?

    Also, on a sidenote, I'm having an issue every time I try to import the facebook SDK, keeps telling me that it has already been imported, even though it hasn't. I haven't been able to find a fix for this, and I would LOVE to make it so you can compete with your facebook friends in my (hopefully) soon to be released Trivia game.

    Any help on these two issues would be greatly appreciated. I can go forward without adding facebook but I feel like it would be an awesome addition to any game!

    Thank you!
     
  2. MathiasDG

    MathiasDG

    Joined:
    Jul 1, 2014
    Posts:
    114
    I'm not familiar with the tutorials you're talking about, but i'll try to help.

    You can make a bitmask for categories and use a GUI bitmask field for that. Check out the GUI documentation on how to do this.
    Code (csharp):
    1.  
    2. [System.Flags]
    3. enum Category {
    4.     Math,
    5.     Sports,
    6.     AnotherCategory
    7. }
    8.  
    And then you give each Question a category field

    Code (csharp):
    1.  
    2. class Question {
    3.     string question;
    4.     string answer;
    5.     Category category;
    6. }
    7.  
    You only add a question to the current round question pool if the player selected categories contain the question category

    Code (csharp):
    1.  
    2. foreach (Question q in allQuestions) {
    3.     if ((playerSelectedCategories & q.category) != 0) {
    4.         currentRoundData.questions.Add (q);
    5.     }
    6. }
    7.  
     
    BillTheThrill likes this.
  3. BillTheThrill

    BillTheThrill

    Joined:
    May 18, 2016
    Posts:
    18
    Okay, I'm going to check out the GUI documentation and any tutorials I may be able to find about the GUI as well.

    Thank you very much for the information! I appreciate it!
     
  4. BillTheThrill

    BillTheThrill

    Joined:
    May 18, 2016
    Posts:
    18
    Okay, so I'm back and I feel like an idiot for not figuring this out.

    But I get the flags part, and I've read the documentation but I still can't figure out how to make the bitmask work.

    I apologize for not doing this in the first place, but if it helps anyone help me figure it out the tutorial I followed as this one:

    https://unity3d.com/learn/live-training/session/creating-quiz-game-session-1

    And again, what I am trying to do is figure out how you click one category (let's say Sports) and get only that categories' questions in the question box, then when done... back to the main menu, player wants to choose different category, (let's say Music this time), so how would I get the music questions to come up this time around, but in the same box as the Sports questions came up in the first round?

    This little roadblock has me really frustrated, so I took a break, came back and tried to figure it out myself again and absolutely no luck at all, so here I am posting and hoping that it's not completely stupid to ask.

    Thank you to anyone who may potentially be able to explain this to me.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Unfortunately, while the answer posted before was the right idea, the code (I'm pretty sure) was incorrect.
    enums with the flag attribute are not truly bit fields. You can make them act like bit fields, but it is not supported in the editor unless you have a custom one (afaik). There is information online about how to get that.
    However, I do not think you want a bitfield anyways. I think an enum would fit better for your game. However, I also think you don't need flags for it.
    * quick explanation: flags -- all that does is print out the enums differently in .ToString() (afaik). And that won't be helping you. Plus, in that context, it could/would be used for mixing values together: for example, blue + yellow (you get green). **

    Okay, so you want to save the user selection, I'd say the easiest solution for this game would be to use a static variable. ( It's not the only option to move data between scenes, but for your game and this variable it sounds very nice).

    In some script (any one you choose), just make :
    Oh, please note that you could choose an enum here if you wanted to. If that seems nicer for you, go for it...
    Enums can be cool because they're kind of like "names" (loosely speaking) :)
    Code (csharp):
    1. public static int ChosenCategory = 0;
    2. // when you set it
    3. ScriptName.ChosenCategory = newNumber;
    4. // accessing it
    5. int number = ScriptName.ChosenCategory;
    6.  
    Enum example:
    Code (csharp):
    1.  
    2. // can be inside or outside the class definition. Only difference is how you access it.
    3. enum Categories {
    4.    Sports,
    5.    Math
    6. }
    7. public static Categories ChosenCategory;
    8. // assign
    9. ScriptName.ChosenCategory = Categories.Sports; // if the enum was declared outside of the class.
    10. // or
    11. ScriptName.ChosenCategory = YourClassName.Categories.Sports; // if it was inside "YourClassName" class.
    12.  
    Re-using the same boxes for different categories & questions is no problem.. So long as you're accessing the right category, you can fill in whatever questions you want in the same UI objects :)

    Hopefully that helps.
     
    BillTheThrill likes this.
  6. BillTheThrill

    BillTheThrill

    Joined:
    May 18, 2016
    Posts:
    18
    Thanks for that information, and I do like the fact that the enums allow me to choose a category for each question. I may be missing something about how to place each question into its appropriate category with what you showed me there, so I have put together a little diagram to hopefully show better what I'm having trouble with and HOPEFULLY this can give show it better than I can explain it. I saw how the enums were allowing me to choose what Category for each question, but then I couldn't figure out how to use them in the buttons then transfer them to the game panel?

     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, you could store the questions (all of them) in another collection. It might be another list or it might be a dictionary. Then, you can use the enum or integer as an index or "key" to the selected (user-chosen) collection for which .. (questions) to display :)
    Does that make sense?
     
    BillTheThrill likes this.
  8. BillTheThrill

    BillTheThrill

    Joined:
    May 18, 2016
    Posts:
    18
    I think I'm too much of a beginner for some of this stuff! haha.

    BUT... I have another idea to do it in the long way basically. I may just make a "DataController" script for each category, like "SportsData" or something, then make each category have it's own panel, simply activate the one that the player chose and leave the rest deactivated.

    I know that's not really the ideal way to do it but I'm pretty sure I'm capable of doing it that way for now.

    Also, I really appreciate the replies! Thank you very much!
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem. I'm like that, too, when I can't figure it out one way, I aim to get it done another way :)

    Even though you're going to try it that way, if you tell me a little about how the game sets up the questions, maybe I could show you an example of what I was talking about .. something you could look over for later, maybe, if you want :)

    Have fun, either way :) Enjoy your game.
     
    BillTheThrill likes this.
  10. BillTheThrill

    BillTheThrill

    Joined:
    May 18, 2016
    Posts:
    18
    Sorry for the late response, I was focusing on what I had planned to do and didn't think to check this again, but here is the ShowQuestion function from the game, which I basically used straight from the tutorial.


    Code (CSharp):
    1.  
    2. private void ShowQuestion()
    3.     {
    4.  
    5.         RemoveAnswerButtons ();
    6.         QuestionData questionData = questionPool [questionIndex];
    7.         questionDisplayText.text = questionData.questionText;
    8.  
    9.         for (int i = 0; i < questionData.answers.Length; i++) {
    10.  
    11.             GameObject answerButtonGameObject = answerButtonObjectPool.GetObject ();
    12.             answerButtonGameObject.transform.SetParent (answerButtonParent);
    13.             answerButtonGameObjects.Add (answerButtonGameObject);
    14.  
    15.             AnswerButton answerButton = answerButtonGameObject.GetComponent<AnswerButton> ();
    16.             answerButton.Setup (questionData.answers [i]);
    17.  
    18.         }
    19.     }
    20.  
    To add onto this, since I'm showing the ShowQuestion function, would you also have any idea how to grab a random question from the "DataController"? Right now the current setup, which was done in the Tutorial, will always grab the questions in a set order, 1, 2, 3, etc. I'd rather it be random each time the player plays, question 5, then 2, then 7, then 4, and so on.

    The questions are being entered in the Inspector, connected to the DataController GameObject, if that helps at all.
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Maybe I will just skip trying to show an example of the type of list (from my last post) since you've been going forward regardless. :)

    As for your random question, I can try to offer a couple of simple ideas.
    1) create a list with each index and just shuffle them around once. When they're at the end of the list, I guess they finished the questions/round or whatever. In my opinion, a very simple and easy shuffle is to go through the list in order, choose a random index (make sure it's not the index your at), and then swap that spot with your current spot. :)

    2) create a list with each index (in order) , choose a random index (whose value you'd use to show the question) from the list and then remove the number at that index (remake the list when it's empty, if the person can keep playing or what not).

    Let me know if either of those makes some sense :)
     
    BillTheThrill likes this.
  12. BillTheThrill

    BillTheThrill

    Joined:
    May 18, 2016
    Posts:
    18
    I've never done either of those options yet, I'm still pretty new to Unity, but I'll definitely look into each option and see if I can do it. I love learning new things in Unity, I get pretty excited when they end up working for me. haha.

    Also, I plan on adding LOTS of questions to each category, so much so that the timer will end long before they reach the end of the question list. That way, they should be able to keep replaying and trying to beat their old high score, while hopefully not getting too many repetitive questions.

    Also, I really appreciate all your replies and help. I can't say thanks enough.

    I haven't attempted those options yet cause I spent most of my time making sure I could do a high score for each category, and I was able to get that all figured out. Had to do some testing and fix a few mistakes, but it works good and looks good so far!
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, I'm glad your game is progressing nicely for you :)
    Good luck with the next parts :)
     
  14. BillTheThrill

    BillTheThrill

    Joined:
    May 18, 2016
    Posts:
    18
    I need to apologize for asking so many questions but at the same time, I greatly appreciate all your replies and information!

    So, anything I can find would only take the "All Round Data" and add it to the list when I only want the questions from each category added to a list.

    I'm not sure I do fully understand this idea. I am assuming that this ShowQuestion function would have to change as well and I'm not sure the List is created with only the "Questions" part (I've posted a screenshot to show exactly what I mean, where I want the questions from) ... So how would I go about changing the ShowQuestion to create a List with only the "Questions" part randomized?


    Code (CSharp):
    1.     private void ShowQuestion()
    2.     {
    3.  
    4.         RemoveAnswerButtons ();
    5.         QuestionData questionData = gamesQuestionPool [gamesQuestionIndex];
    6.         questionDisplayText.text = questionData.questionText;
    7.  
    8.         for (int i = 0; i < questionData.answers.Length; i++) {
    9.  
    10.             GameObject answerButtonGameObject = answerButtonObjectPool.GetObject ();
    11.             answerButtonGameObject.transform.SetParent (answerButtonParent);
    12.             answerButtonGameObjects.Add (answerButtonGameObject);
    13.  
    14.             AnswerButton answerButton = answerButtonGameObject.GetComponent<AnswerButton> ();
    15.             answerButton.Setup (questionData.answers [i]);
    16.  
    17.         }
    18.     }
     

    Attached Files:

  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If I understand the code, I think what you can do is create a list by one of the two methods I mentioned before, and use that list (including how I wrote to sort it or traverse it/choose from it), and use the new value you get there to give you a modified "gamesQuestionIndex".
    Does that kinda make sense?

    That was my best guess :)