MV External text file with choices of answer?

Hello all,

I am new in using RPG Maker MV and currently trying to make a quiz system in my project. I learned how the show text and show choices works in the game itself but, is there a way on using a list of questions with its choices of answer in an external text file and load it in the game via script? (I'm not good in programming either but I'm trying to learn.)

Thank you in advance if anyone will see my post.
The answer to your question is yes, it would be possible to write a JavaScript plugin that would read what you entered in a specially-formatted text file and displayed it as on-screen choices.

The question would be...why do you want to do this? The time spent writing out that file could instead be used to simply enter the choices as regular event commands.

And, as you see from the event editor, in order to do anything in response to a choice, it goes inside of areas labeled by what the choice is...so to replace that, you'd have to write an even more complicated replacement system via coding.

So I would suggest that what you want to do is possible but impractical, and if you explain why you're thinking it, we can help you with alternatives.

For example, if you wanted to randomize which questions are being asked, you can do that via events - each question (or set of questions or whatever) is in its own common event, then the quiz randomly chooses a common event to call and process the questions.
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.
@Nolonar - This is a great post. Is there any way you can copy this to tutorials or even learning javascript?
if its played on web, players wont be able to access the json file. but if its downloadable.. can they access open and edit it?
if its played on web, players wont be able to access the json file. but if its downloadable.. can they access open and edit it?
I mean, yeah, but they can already do that with any of the other data files as well. At that point they're only really cheating themselves.
:LZSsmile:
Thank you everyone for taking the time to give your insights and suggestions.

@Nolonar Thank you so much for this awesome suggestion/recommendation.
I will probably look into this and study it.
@Nolonar - This is a great post. Is there any way you can copy this to tutorials or even learning javascript?
I'm feeling too lazy for that, but you're free to post it yourself, if you want.

if its played on web, players wont be able to access the json file. but if its downloadable.. can they access open and edit it?
Actually, even if it's played on the web, the players can still access and download the data files if they know the path and file name. This can easily be discovered using the dev tools, though I don't think the average player actually knows how to do it.

Even if they edit the file, they shouldn't be able to upload their edits back to the server, though. If they can, your server is not configured properly and you have a serious security flaw. For example, someone could smuggle a crypto-miner or some other kind of malware into your game, which would affect all your players.

When played locally, the player can easily read and edit the file, including all the other data files, as @Trihan already mentioned. You could try to encrypt those files, though I don't know if RPG Maker MV/MZ encrypts the data files (I believe it doesn't). Alternatively, you can try to use Base64 encoding, but then you'll need to implement your own version of DataManager.loadDataFile() so it'll decode the file first before parsing the JSON. This won't stop people from datamining your game, but the average player (probably) won't know what to do with a Base64 string.

Thank you everyone for taking the time to give your insights and suggestions.

@Nolonar Thank you so much for this awesome suggestion/recommendation.
I will probably look into this and study it.
You're welcome.

Fair warning: I haven't tested my code yet. If you encounter any errors, feel free to ask.
there was a demo by starbird [MVQuizzer] but the download is removed, but the file for it is there,
while it cannot be used in public unless granted.

there are 2 files, 1 plugin to run the other plugin with questions,
but I think the above works easier while there is no GUI on the MVQuizzer plugin.

but there are some bugs which I dont know if they fixed it, I have the demo by the time
and looks good but not always workin correctly.

I think the solution above is much easier, specially if you know how to shuffle it.
otherwise event it (which I did), but I need to make the maps still and implent the rest which is easy.
Even if they edit the file, they shouldn't be able to upload their edits back to the server, though. If they can, your server is not configured properly and you have a serious security flaw. For example, someone could smuggle a crypto-miner or some other kind of malware into your game, which would affect all your players.
If they're just trying to cheat or something, they actually wouldn't need to upload it back to the server. There are ways to trick your browser into using the file from a different location (i.e. local storage, or another server entirely), instead of using the one from the game's server. Alternatively, they could just skip editing the file altogether, and just modify $dataQuiz directly through the console, since it would be a global variable in this case. If they were feeling extra frisky, they could probably hook JSON.parse, and inject their modifications in real time as the file gets loaded. There's really a lot of ways to cheat, even if it's on the web.
I'm feeling too lazy for that, but you're free to post it yourself, if you want.


Actually, even if it's played on the web, the players can still access and download the data files if they know the path and file name. This can easily be discovered using the dev tools, though I don't think the average player actually knows how to do it.

Even if they edit the file, they shouldn't be able to upload their edits back to the server, though. If they can, your server is not configured properly and you have a serious security flaw. For example, someone could smuggle a crypto-miner or some other kind of malware into your game, which would affect all your players.

When played locally, the player can easily read and edit the file, including all the other data files, as @Trihan already mentioned. You could try to encrypt those files, though I don't know if RPG Maker MV/MZ encrypts the data files (I believe it doesn't). Alternatively, you can try to use Base64 encoding, but then you'll need to implement your own version of DataManager.loadDataFile() so it'll decode the file first before parsing the JSON. This won't stop people from datamining your game, but the average player (probably) won't know what to do with a Base64 string.


You're welcome.

Fair warning: I haven't tested my code yet. If you encounter any errors, feel free to ask.
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.

I'm feeling too lazy for that, but you're free to post it yourself, if you want.


Actually, even if it's played on the web, the players can still access and download the data files if they know the path and file name. This can easily be discovered using the dev tools, though I don't think the average player actually knows how to do it.

Even if they edit the file, they shouldn't be able to upload their edits back to the server, though. If they can, your server is not configured properly and you have a serious security flaw. For example, someone could smuggle a crypto-miner or some other kind of malware into your game, which would affect all your players.

When played locally, the player can easily read and edit the file, including all the other data files, as @Trihan already mentioned. You could try to encrypt those files, though I don't know if RPG Maker MV/MZ encrypts the data files (I believe it doesn't). Alternatively, you can try to use Base64 encoding, but then you'll need to implement your own version of DataManager.loadDataFile() so it'll decode the file first before parsing the JSON. This won't stop people from datamining your game, but the average player (probably) won't know what to do with a Base64 string.


You're welcome.

Fair warning: I haven't tested my code yet. If you encounter any errors, feel free to ask.
Hi @Nolonar I tried the code but I am getting an error.
Will you be able to help me out? Thank you in advance.
Please bare with me as I am really a newbie.
The answer to your question is yes, it would be possible to write a JavaScript plugin that would read what you entered in a specially-formatted text file and displayed it as on-screen choices.

The question would be...why do you want to do this? The time spent writing out that file could instead be used to simply enter the choices as regular event commands.

And, as you see from the event editor, in order to do anything in response to a choice, it goes inside of areas labeled by what the choice is...so to replace that, you'd have to write an even more complicated replacement system via coding.

So I would suggest that what you want to do is possible but impractical, and if you explain why you're thinking it, we can help you with alternatives.

For example, if you wanted to randomize which questions are being asked, you can do that via events - each question (or set of questions or whatever) is in its own common event, then the quiz randomly chooses a common event to call and process the questions.
Hello @AAT_Turan Thank you for your reply. Actually I wanted to make a game with a game mode option where the player or the user can provide or add their own sets of questions by saving it in a text file and they can play the game as a quiz reviewer . Do you have any suggestions that might make this possible? I know this is like a stupid idea but I wanted to try.

Thank you so much in advance.
Hi @Nolonar I tried the code but I am getting an error.
Will you be able to help me out? Thank you in advance.
Please bare with me as I am really a newbie.
Sorry about that.

The loading of the data file happens asynchronously (meaning the game keeps running while the file is loading). You're getting this error because the quiz file hasn't finished loading yet, but you're already trying to access it.

The simplest way (though by far not the best) is to add a Wait between Script and Control Variables.
Also, I forgot to mention it, but if you allow the player to save in the middle of a quiz event, you'll need to ensure the data file gets loaded after saving. That's because if the player chooses to exit the game and continue playing another time, the data file will no longer be loaded (because data files aren't saved and loaded with the save game; they are usually loaded when the game is launched, but in this case are loaded during an event).

The best way to solve this issue is to use a plugin to load your data file. Fortunately, the plugin is a simple one:
JavaScript:
DataManager._databaseFiles.push({ name: '$dataQuiz', src: 'Quiz.json' });
Copy this code into a file with ending .js and place it in your /js/plugins folder. Don't forget to add the plugin to your plugin list using the Plugin Manager in the RPG Maker editor.

Using the plugin solution has following advantages:
  • You no longer need to call DataManager.loadDataFile("$dataQuiz", "Quiz.json"); when the event starts, or whenever the player can save (if they can save in the middle of a quiz event).
  • You no longer need to add a Wait after loading the data file, because the file should have already loaded by the time the player hits "New Game" or "Continue".
  • You no longer need to guess how long the event has to Wait, and no longer risk the game crashing if a player's computer is slower than expected. This point is especially important if the game is deployed for the web, because the player's internet connection can further increase the loading delay.

Another thing I forgot to mention: It's possible that your question text (or the Quizmaster's comment) is too long to fit in a single line of text. In that case, you can split the text over multiple lines using \n. In our example Quiz.json this would 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.\nIncidentally, the Earth is the third planet."
            },
            {
                "text": "Milky Way",
                "isCorrect": true,
                "quizmasterComment": "Yes. It's called \"Milky Way\" because of how it looks\nfrom Earth!"
            },
            {
                "text": "Alpha Centauri",
                "isCorrect": false,
                "quizmasterComment": "No. Alpha Centauri refers to the three closest stars\nto our Solar System. They are Alpha Centauri A (Rigil\nKentaurus), Alpha Centauri B (Toliman), and Alpha\nCentauri C (Proxima Centauri)."
            }
        ]
    },
    {
        "question": "Which of these is a country?",
        "answers": [
            {
                "text": "Belgium",
                "isCorrect": true,
                "quizmasterComment": "Correct! It's a country in Western Europe.\nIt neighbors Germany, France, the Netherlands,\nand 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."
            }
        ]
    }
]

Any character that has a backslash \ in front is called an "escape character". \n is the escape character for a new line, and I'm sure you noticed \", which is also an escape character used to display the double quote mark ("). You only need \" when your string is wrapped in double quote marks (example: "The name's \"John\"!"). JavaScript (and JSON) also allows you to wrap strings in single quote marks ('), in which case you can use double quote marks without the escape character (example: 'The name\'s "John"!'). Obviously, you need to escape the single quote in this case.

For more escape characters, you can refer to this site: https://www.tutorialspoint.com/escape-characters-in-javascript

Although the most important ones are these:
  • \n: new line
  • \': single quote
  • \": double quote
  • \\: backslash (if you need to display a single \ in your text)
Sorry about that.

The loading of the data file happens asynchronously (meaning the game keeps running while the file is loading). You're getting this error because the quiz file hasn't finished loading yet, but you're already trying to access it.

The simplest way (though by far not the best) is to add a Wait between Script and Control Variables.
Also, I forgot to mention it, but if you allow the player to save in the middle of a quiz event, you'll need to ensure the data file gets loaded after saving. That's because if the player chooses to exit the game and continue playing another time, the data file will no longer be loaded (because data files aren't saved and loaded with the save game; they are usually loaded when the game is launched, but in this case are loaded during an event).

The best way to solve this issue is to use a plugin to load your data file. Fortunately, the plugin is a simple one:
JavaScript:
DataManager._databaseFiles.push({ name: '$dataQuiz', src: 'Quiz.json' });
Copy this code into a file with ending .js and place it in your /js/plugins folder. Don't forget to add the plugin to your plugin list using the Plugin Manager in the RPG Maker editor.

Using the plugin solution has following advantages:
  • You no longer need to call DataManager.loadDataFile("$dataQuiz", "Quiz.json"); when the event starts, or whenever the player can save (if they can save in the middle of a quiz event).
  • You no longer need to add a Wait after loading the data file, because the file should have already loaded by the time the player hits "New Game" or "Continue".
  • You no longer need to guess how long the event has to Wait, and no longer risk the game crashing if a player's computer is slower than expected. This point is especially important if the game is deployed for the web, because the player's internet connection can further increase the loading delay.

Another thing I forgot to mention: It's possible that your question text (or the Quizmaster's comment) is too long to fit in a single line of text. In that case, you can split the text over multiple lines using \n. In our example Quiz.json this would 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.\nIncidentally, the Earth is the third planet."
            },
            {
                "text": "Milky Way",
                "isCorrect": true,
                "quizmasterComment": "Yes. It's called \"Milky Way\" because of how it looks\nfrom Earth!"
            },
            {
                "text": "Alpha Centauri",
                "isCorrect": false,
                "quizmasterComment": "No. Alpha Centauri refers to the three closest stars\nto our Solar System. They are Alpha Centauri A (Rigil\nKentaurus), Alpha Centauri B (Toliman), and Alpha\nCentauri C (Proxima Centauri)."
            }
        ]
    },
    {
        "question": "Which of these is a country?",
        "answers": [
            {
                "text": "Belgium",
                "isCorrect": true,
                "quizmasterComment": "Correct! It's a country in Western Europe.\nIt neighbors Germany, France, the Netherlands,\nand 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."
            }
        ]
    }
]

Any character that has a backslash \ in front is called an "escape character". \n is the escape character for a new line, and I'm sure you noticed \", which is also an escape character used to display the double quote mark ("). You only need \" when your string is wrapped in double quote marks (example: "The name's \"John\"!"). JavaScript (and JSON) also allows you to wrap strings in single quote marks ('), in which case you can use double quote marks without the escape character (example: 'The name\'s "John"!'). Obviously, you need to escape the single quote in this case.

For more escape characters, you can refer to this site: https://www.tutorialspoint.com/escape-characters-in-javascript

Although the most important ones are these:
  • \n: new line
  • \': single quote
  • \": double quote
  • \\: backslash (if you need to display a single \ in your text)
Hello @Nolonar Thank you for helping a lot! But I just need this one more question, one more issue I am facing is when I try to retrieve the $gameVariables.setValue(1,$dataQuiz[0].question); and same with the answers, the text showing is 0 not the text question itself. I tried to troubleshoot both plugin and .json file but still the same issue. Is there another way to fix it?

Thank you again!
Hello @Nolonar Thank you for helping a lot! But I just need this one more question, one more issue I am facing is when I try to retrieve the $gameVariables.setValue(1,$dataQuiz[0].question); and same with the answers, the text showing is 0 not the text question itself. I tried to troubleshoot both plugin and .json file but still the same issue. Is there another way to fix it?

Thank you again!
If you're still trying to do it the same way that you did in the screenshot that you posted earlier, then I'm pretty sure that's the source of your problem. The $gameVariables.setValue(1,$dataQuiz[0].question); statement is supposed to be used in a regular script call, not a "Control Variables" script call.

You did this:
◆Control Variables:#0085 = $gameVariables.setValue(85,$dataQuiz[0].question);

But you need to do this:
◆Script:$gameVariables.setValue(85, $dataQuiz[0].question);

Alternatively, you could probably do it like this:
◆Control Variables:#0085 = $dataQuiz[0].question

The way you're doing it right now, I'm pretty sure is going to initially put the value that you want into variable 85, but then it's going to immediately overwrite that value with the return value of the 'setValue' method. IIRC, 'setValue' doesn't have an explicit return value, so I reckon it would return 'null' every time.
Hello @Nolonar Thank you for helping a lot! But I just need this one more question, one more issue I am facing is when I try to retrieve the $gameVariables.setValue(1,$dataQuiz[0].question); and same with the answers, the text showing is 0 not the text question itself. I tried to troubleshoot both plugin and .json file but still the same issue. Is there another way to fix it?

Thank you again!
@Arthran is correct.

The reason you're getting a 0, is because Control Variables already uses $gameVariables.setValue() internally, so what you're doing is comparable to:
JavaScript:
$gameVariables.setValue(85, $gameVariables.setValue(85,$dataQuiz[0].question));

And since $gameVariables.setValue() doesn't have a return value, that results in:
JavaScript:
$gameVariables.setValue(85, undefined);

RPG Maker MV/MZ then automatically converts undefined to 0 before saving it to Variable #0085, therefore overwriting the quiz question that you had just written to it with 0.

The solution has already been provided, but here's how I implemented it, if you're interested:

  1. I use an Autorun event that calls a common event for the quiz (this allows me to reuse the quiz mechanics in different parts of the game, without having to copy-paste the code).

    Screenshot 2021-11-20 164417.png


  2. The "Do quiz" common event prepares a couple of variables:
    • #0001: The question currently being asked (starting with question 0, the first question).
    • #0002: How many questions will be asked in total. In this case, all questions in the data file will be asked, but if your data file has lots of questions, you might not want to ask them all.
    • #0009: The player's score (in this example, I'm keeping track of the score, but I'm never actually using it)

    It then "asks the questions" in a loop, increases Variable #0001 to move to the next question, and stops when Variable #0001 has become equal to Variable #0002.

    I moved the "Ask question" code to its own common event for aesthetic reasons (mostly because the code wouldn't fit in the window otherwise; it still doesn't fit completely, though).

    Screenshot 2021-11-20 164438.png


  3. The "Ask question" common event shows the question text, provides a choice using the possible answers, and saves the result in Variable #0007 and the quizmaster's comment in Variable #0008 before "checking the answer". Actually, I could've reused Variable #0003 for the quizmaster's comment, but I forgot...

    I moved the "Check answer" code to its own event, because I'm using it 3 times. Now that I think about it, I could've just moved it outside of the Show Choices - When block, then I wouldn't need to use it 3 times, and wouldn't need to waste an entire common event for that. Oh well...

    Screenshot 2021-11-20 164447.png


    PS: I just learned that if Show Choices does not immediately follow Text (in this case, I have a couple of Control Variables in between), then the question will disappear as soon as the player can answer. The player can see the answers, but they can't re-read the question if they forget what the question was supposed to be. This may or may not be what you want.

    You can easily fix this by moving the Text command to right before the Show Choices command.

    Screenshot 2021-11-20 172425.png


  4. Finally, the "Check answer" common event simply checks if Variable #0007 was true (non-zero) or false (zero), and increases the player's score accordingly. It then display's the quizmaster's comment.

    Screenshot 2021-11-20 164455.png
I hope this was helpful.
@Nolonar and @Arthran Thank you so much! I cannot thank you enough for this!
I will continue to study and learn more about all of this.

Thank you Thank you
Hello all,

I am new in using RPG Maker MV and currently trying to make a quiz system in my project. I learned how the show text and show choices works in the game itself but, is there a way on using a list of questions with its choices of answer in an external text file and load it in the game via script? (I'm not good in programming either but I'm trying to learn.)

Thank you in advance if anyone will see my post.
Sorry, just saw this. I can send you my plugin. Removed it because I'm working on an MZ version and didn't want there to be confusion over versions/compatibility

Original thread: https://forums.rpgmakerweb.com/threads/external-text-file-with-choices-of-answer.141968/