First, you'll need to define the structure of your text file, so your script can tell the difference between questions, answers, and the correct answer.
I highly recommend using JSON for that. Your structure would likely look like this:
JSON:
[
{
"question": "What is the name of the galaxy in which we live?",
"answers": [
{
"text": "Mars",
"isCorrect": false,
"quizmasterComment": "No, that's the fourth planet of our Solar System. Incidentally, the Earth is the third planet."
},
{
"text": "Milky Way",
"isCorrect": true,
"quizmasterComment": "Yes. It's called \"Milky Way\" because of how it looks from Earth!"
},
{
"text": "Alpha Centauri",
"isCorrect": false,
"quizmasterComment": "No. Alpha Centauri refers to the three closest stars to our Solar System. They are Alpha Centauri A (Rigil Kentaurus), Alpha Centauri B (Toliman), and Alpha Centauri C (Proxima Centauri)."
}
]
},
{
"question": "Which of these is a country?",
"answers": [
{
"text": "Belgium",
"isCorrect": true,
"quizmasterComment": "Correct! It's a country in Western Europe. It neighbors Germany, France, the Netherlands, and Luxembourg."
},
{
"text": "Africa",
"isCorrect": false,
"quizmasterComment": "Wrong, that's a continent to the South of Europe."
},
{
"text": "Paris",
"isCorrect": false,
"quizmasterComment": "Wrong, that's a city, and the capital of France."
}
]
}
]
You can name that file "Quiz.json" for example.
The reason I recommend this, is because you can easily modify the structure to suit your needs. For example, you can easily remove the "quizmasterComment" both from the text file, as well as from the game code. You can just as easily add more properties, if necessary.
The easiest way to load this file would be by using the
DataManager.loadDataFile("$dataQuiz", "Quiz.json"); script command. If you don't like the name "$dataQuiz", you can choose any name that suits your needs, such as "myQuizData" or similar. Since you're a beginner, though, I recommend sticking to the "$dataQuiz" name, to avoid conflicts with other existing names (which could lead to weird bugs or crashes). Obviously, "Quiz.json" must match the name of your JSON file. If you named your JSON file
foo.bar, then you need to use "foo.bar" instead.
Make sure your "Quiz.json" file is located in the
/data directory of your project. That same directory should contain various .json files, such as "Actors.json", "Animations.json", and so on.
Now that you've loaded the file, you can access it using the name you defined (in this case
$dataQuiz). For example, to retrieve the first entry, you call:
$dataQuiz[0], for the second entry:
$dataQuiz[1], for the third one:
$dataQuiz[2], and so on.
If you want to select questions at random, then I recommend shuffling the
$dataQuiz array to avoid asking the same question multiple times in a single quiz. You can refer to this StackOverflow question on how to shuffle an array:
https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array
Once shuffled, you can simply access the questions in order (which will appear random, as it's been shuffled).
To display the question as a text, you'll need to store the "question" text in a variable. Let's say you want to use Variable #0001:
JavaScript:
$gameVariables.setValue(1, $dataQuiz[0].question);
will store
"What is the name of our galaxy?" into Variable #0001 (assuming the array hasn't been shuffled). You can display it by writing
\v[1] in the
Show Text... event command.
You'll need to do something similar for the
Show Choices... event command.
Let's say you'll use Variable #0002 to Variable #0004 for the 3 choices:
JavaScript:
$gameVariables.setValue(2, $dataQuiz[0].answers[0].text);
will store
"Mars" in Variable #0002,
JavaScript:
$gameVariables.setValue(3, $dataQuiz[0].answers[1].text);
will store
"Milky Way" in Variable #0003, and
JavaScript:
$gameVariables.setValue(4, $dataQuiz[0].answers[2].text);
will store
"Alpha Centauri" in Variable #0004.
You can display those in your choices using
\v[2],
\v[3], and
\v[4] respectively.
You can determine if the answer was correct using a conditional branch with the following script:
If the player chose answer 1:
JavaScript:
$dataQuiz[0].answers[0].isCorrect
If they chose answer 2 instead:
JavaScript:
$dataQuiz[0].answers[1].isCorrect
If they chose 3:
JavaScript:
$dataQuiz[0].answers[2].isCorrect
and so on.
If you want the quiz master to make a comment, you can store that comment in the same variable where you stored the question (since you no longer need the question any more):
JavaScript:
$gameVariables.setValue(1, $dataQuiz[0].answers[0].quizmasterComment);
This will store
"No, that's the fourth planet of our Solar System. Incidentally, the Earth is the third planet." in Variable #0001.