- Home /
Linking button press with questions
Hey guys, Im making a board game which involves multiple choice questions to move forward. this is the script which displays the questions:
var arrayOfQuestions : Question [];
class Question
{
var questionText : String;
var answerText1 : String;
var answerText2 : String;
var answerText3 : String;
var answerText4 : String;
var correctAnswer : String;
/* example constructor (creates a Question from two params) */
function Question ( q : String, a : String, b : String, c : String, d : String, correct : String )
{
questionText = q;
answerText1 = a;
answerText2 =b;
answerText3 =c;
answerText4=d;
correctAnswer = correct;
}
}
/* initialize the question array in the start function */
function Start ()
{
arrayOfQuestions = new Question [2];
/* use a for loop to create dummy questions */
arrayOfQuestions[0] = new Question("Where are we?", "E6", "K8", "G1", "MoCap", "K8");
arrayOfQuestions[1] = new Question("Where are we?", "E6", "K8", "G1", "MoCap", "K8");
}
/* 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.answerText1 + " B: " + thisQ.answerText2 + " C: " + thisQ.answerText3 + " D:" + thisQ.answerText4;
GUI.Label(labelRect, txt);
labelRect.y += labelRect.height;
}
}
i was just wondering how do i link a button press to it?
Answer by robertbu · Feb 04, 2014 at 07:43 PM
Putting the text on buttons as @Nanobrain suggests is one good approach. No matter how you do it, I recommend restructuring your possible answers into an array as @Nanobrain has in his example code. Another approach is to make your label text clickable. As with the buttons, that means you are going to have to display each possible answer individually rather than combining all the Q/A into a single string. Here is a bit of code to demonstrate how to make the text clickable. I uses Rect.Contains() to detect a mouse click within the Rect used to display the text. In a new scene, attach the script to an empty game object and run:
#pragma strict
private var question = "Where does he live?";
private var possibleAnswers = ["Paris", "London", "New York", "Mexico City"];
private var labels = [" A: ", " B: ", " C: ", " D: "];
private var questionRect = Rect(200,200,400,30);
private var answerRects = [Rect(200, 235, 400, 25), Rect(200, 270, 400, 25), Rect(200, 305, 400, 25), Rect(200, 340, 400, 25)];
function OnGUI() {
var e = Event.current;
GUI.Label(questionRect, question);
if (e.type == EventType.Repaint) {
for (var i = 0; i < possibleAnswers.Length; i++) {
GUI.Label(answerRects[i], labels[i]+possibleAnswers[i]);
}
}
// Check the mouse click
if (e.type == EventType.MouseDown) {
for (i = 0; i < possibleAnswers.Length; i++) {
if (answerRects[i].Contains(e.mousePosition)) {
Debug.Log("The user clicked on answer #" + i);
break;
}
}
}
}
then all i need to do is put an if statement to show the correct answer ? Would i need to repeat this if i was going to have 20 questions ?
Your Question class is in the right direction, and as you've done you will want to have an array of these questions. You are going to have to have something that calculates the position answer either for button placement as @Nanobrain as done or for hit testing as I've done. Painting and hit testing would be done in another 'for()' loop with the loops I have above as the inner loop.
I liked the way you did it @robertbu with the click, that will do perfectly fine for what im doing. im just a little confused on how to set up multiple questions and clcking the right answer. I know the array will allow me to store more questions,its just trying to merge it with what youve done in the example code you posted
ive done a few more questions, do i just put the correct answer inside the function OnGUI function Update () { arrayOfQuestions = new Question [5];
arrayOfQuestions[0] = new Question("How many league titles have Liverpool Won?", "5", "9", "14", "18", "18");
arrayOfQuestions[1] = new Question("Who won the World Cup in 2010", "Germany", "Italy", "Brazil", "Spain", "Spain");
arrayOfQuestions[2] = new Question("Who is the $$anonymous$$anager of $$anonymous$$anchester United","$$anonymous$$ $$anonymous$$oyes","Rafa Benitez","Sir $$anonymous$$ Fergerson","Brendan Rodgers","$$anonymous$$ $$anonymous$$oyes");
arrayOfQuestions[3] = new Question("The starting lineup for a football $$anonymous$$m is consisted of how many players?","9","15","10","11","11");
arrayOfQuestions[4] = new Question("How many European titles have Liverpool won","7","9","5","3","5");
}
@robertbu im trying to do same method as you did with the mouse click inside the function OnGUI but completely lost.
function OnGUI () { var labelRect : Rect = Rect(0,90,400,75); for ( var thisQ : Question in arrayOfQuestions ) { /*var txt = "Q: " + thisQ.questionText + " A: " + thisQ.answerText1 + " B: " + thisQ.answerText2 + " C: " + thisQ.answerText3 + " D:" + thisQ.answerText4; GUI.Label(labelRect, txt); labelRect.y += labelRect.height;*/ } }
I need to somehow link the correctAnswer inside here and click it, the correct answer is on the end of each of the array questions
Answer by Nanobrain · Feb 04, 2014 at 07:00 PM
There's a few ways this could be done, depending on what results you're wanting. One way would be to add your answer texts each as the text for a button.
Button(answerButtonRect, answerText1);
And so you could position the buttons wherever you wish, however your layout determines.
To detect a click on the button place it in an IF statement. If the result equals 'true' then the button was pressed that frame.
if(new Button(answerButtonRect, answerText1)) {
// place code here to execute when answer 1 is pressed
}
That said, your answers should be placed in an array as well, so that you can iterate through them and place a button on each one of them more easily and with less, more readable, sustainable code.
// please mind any syntax errors, I code using C#
for(var index:int = 0; index < answerText.length; index ++) {
if(new Button(answerButtonRect, answerText[index])) {
// execute code for selection # index
// maybe a switch statment? Up to you.
}
}
Im new to scripting, so its going to take me a while to understand. can you show me how you would do it for the example question thats in my code, so i can understand the process.
First, create your question and answer classes:
class Question {
public string questionText; // the text of the question
public Answer[] answers; // array of answer objects
public int correctAnswer; //integer deter$$anonymous$$ing at what index in the answer array is considered the correct answer for this question
public Question(string theQuestionText, Answer[] theAnswers, int theCorrectAnswer) {
questionText = theQuestionText;
answers = theAnswers;
correctAnswer = theCorrectAnswer;
}
public draw() {
// gui code goes here
}
}
class Answer {
public answerText;
public Answer(theAnswerText) {
answerText = theAnswerText;
}
}
Then create your array of questions within your main script:
// within the start function of your main script
Question[] questions = new Array();
questions.push(new Question(
"Question text for question #1",
new Array(
new Answer("Answer #1"),
new Answer("Answer #2"),
new Answer("Correct answer"),
new Answer("Answer #4")
),
2)
);
questions.push(new Question(
"Question text for question #2",
new Array(
new Answer("Correct answer"),
new Answer("Answer #2"),
new Answer("Answer #3"),
new Answer("Answer #4")
),
0)
);
questions.push(new Question(
"Question text for question #3",
new Array(
new Answer("Answer #1"),
new Answer("Answer #2"),
new Answer("Answer #3"),
new Answer("Correct answer")
),
3)
);
Now focus on how you will layout your question gui:
// this is the draw function within the question class
draw() {
int top = 100;
// layout the question text
new GUI.Label(new Rect(100, top, 300, 100), questionText);
// layout your answers
for(int i = 0; i < answers.length; i ++) {
top += 100;
if(new GUI.Button(new Rect(100, top, 300, 100), answers[i].answerText)) {
if(i == correctAnswer) {
// execute code for pressing this answer button
// what happens here depends on the overall
// flow of your program, your intentions after the correct
// answer is selected. $$anonymous$$aybe incrementing the current active question
// so that the next question will be displayed
}
}
}
}
Lastly, in your main script, in it's onGUI method, you should call the draw function of the currently active question.
onGUI() {
questions[currentQuestionIndex].draw();
}
Answer by Mint92 · Feb 05, 2014 at 12:06 AM
shall i do the same lay out as i did for the array of questions, like this:
var arrayOfQuestions : Question []; var arrayOfAnswers: Answers [];
class Question
{
var questionText : String;
var answerText1 : String;
var answerText2 : String;
var answerText3 : String;
var answerText4 : String;
function Question ( q : String, a : String, b : String, c : String, d : String)
{
questionText = q;
answerText1 = a;
answerText2 =b;
answerText3 =c;
answerText4=d;
}
}
class Answers
{
var answerText1 : String;
var answerText2 : String;
var answerText3 : String;
var answerText4 : String;
var correctAnswer : String;
function Answers (a : String, b : String, c : String, d : String, correct : String )
{
answerText1 = a;
answerText2 =b;
answerText3 =c;
answerText4=d;
correctAnswer = correct;
}
}