- Home /
Storing questions/answers for a multiple choice game
Hi all,
Noob here :) I was wondering what recommendations you would have about a good way to store a sizable amount of questions (and answers) for a multiple-choice type game.
If it were just 10-15 questions I might hard code them, but if I want to have many more, I imagine there's a better solution.
Maybe in to an array? Could I create some type class to define each question/answer?
Thanks, any help or pointers are appreciated!
Answer by jahroy · Dec 17, 2011 at 11:54 PM
Here's a very simple example of how you would create a class named Question.
It demonstrates how you would use a for loop to initialize the array full of dummy values.
It then tests the data by printing a bunch of labels with a for loop.
I'm sure there are errors... I just typed it up real quick.
If there are no errors, you should be able to use this script as is.
A more practical approach would be to get rid of the Start function (it's only for an example) and define your questions in the Inspector.
If you add that script to a GameObject, you should see a slot in the Inspector named Array Of Questions. You can populate this by entering a size then typing text into the slots named Question Text and Answer Text for each element in the array.
Hope this helps.
var arrayOfQuestions : Question [];
class Question { var questionText : String; var answerText : String;
/* example constructor (creates a Question from two params) */
function Question ( q : String, a : String )
{
questionText = q;
answerText = a;
}
}
/ initialize the question array in the start function /
function Start () { arrayOfQuestions = new Question [20];
/* use a for loop to create dummy questions */
for ( var i = 0; i < 20; i ++ ) {
var someQ : String = "Question number: " + i;
var someA : String = "Answer number: " + i;
/* use the example constructor from above */
var freshQuestion : Question = new Question(someQ, someA);
arrayOfQuestions[i] = freshQuestion;
}
}
/ test it /
function OnGUI () { var labelRect : Rect = Rect(200, 200, 300, 30);
for ( var thisQ : Question in arrayOfQuestions ) {
var txt = "Q: " + thisQ.questionText + " A: " + thisQ.answerText;
GUI.Label(labelRect, txt);
labelRect.y += labelRect.height;
}
}
Wow, this is a HUGE help! Thanks for the explanation. A follow-up question, regarding your suggestion: how would I go about initializing the array with the size and entries that I set in the inspector?
For example, when I use: arrayOfQuestions = new Question [20]; ... it's creating a new array with 20 empty slots in it, as opposed to the number (and entries) that I've specified in the inspector. If I understand that correctly, of course. ;)
Oh wait... it seems that it does already initialize the array, and I can access it directly without having to create a new empty one in the script. Awesome!
I think you understand perfectly.
When you enter a size in the Inspector, some code behind the scenes must call something that looks very much like this:
exampleTransformArray = new Transform [numberFromInspector];
You $$anonymous$$UST allocate memory for a builtin array before you start to populate or play with it
(unless you let the Inspector do it for you).
There are also Dynamic Arrays available in javascript that you can resize on the fly.
You can play with a dynamic array, get it dialed-in the way you like it, and convert it to a builtin array, too.
A good place to do this is in Editor scripts (where performance doesn't matter very much). That's how you can initialize an array of an unknown size and turn it into a builtin, typed array.
For example, you might have a million questions listed in a text file. You could read them in with an editor script and assign them to an array in a script.
Unity makes this VERY easy with TextAssets:
http://answers.unity3d.com/questions/131720/jahroy-text-asset-toot.html
Boy... Unity is pretty cool.
Hi,
I'm new to Unity. Is there a C# version of the above function? I want to try it out for a multiple choice game.
Any help or guidance is appreciated.
Thanks!
C# and UnityScript are quite similar. If you understand one of them, it should be trivial to convert. If you don't understand one of them, I can't think of a better way to learn than to give it a try. If you rely on others to do something this simple for you, you'll never learn anything.
I'll give you one hint. In UnityScript you declare a Rect like this:
var someRect : Rect = Rect(200, 200, 100, 100);
The same declaration in C# would look like this:
Rect someRect = Rect(200, 200, 100, 100);
C# does not require the var keyword and the type of the variable comes before its name (in s$$anonymous$$d of co$$anonymous$$g after it and being separated by a colon).
Just keep your eye on the errors in the console (in red) and do some googling and you'll figure it out real quick. Good luck!
Your answer
Follow this Question
Related Questions
Can't destroy an arrray with GUI Text 1 Answer
Move the text in a GUI Button/Box 1 Answer
GUI.label overlapping text 1 Answer
Gui Text Script 4 Answers
Alpha not working in GUITex 0 Answers