- Home /
Spawning random questions at a time
Hey guys, how do you go abouts spawning questions one at a time and randomly. the questions have been set up as an array same with the answers. but its showing all the questions straight away var arrayOfQuestions : Question [];
class Question
{
private var questionText : String;
private var questionRect : Rect;
private var answers : String[] = new String[4];
private var displayAnswers: String[] = new String[4];
private var answerRects : Rect[] = new Rect[4];
private var correctAnswer : String;
private var correctRect : Rect;
private var correctAnswerIndex : int; // Zero based
private var questionWidth =300;
private var answerWidth = 75;
private var questionHeight = 1000;
private var correctWidth = 500;
private var selectedAnswer = -1;
// Constructor
function Question (pos : Vector2, q : String, a : String, b : String, c : String, d : String, correct : int )
{
questionText = q;
answers[0] = a;
answers[1] = b;
answers[2] = c;
answers[3] = d;
var st = "ABCD";
for (var i = 0; i < answers.Length; i++) {
displayAnswers[i] = st[i]+": "+answers[i];
}
correctAnswerIndex = correct;
correctAnswer = "Correct answer: " + answers[correct];
CalcRects(pos);
}
// Calculate all the rectangles used for display
function CalcRects(pos : Vector2)
{
var rect : Rect;
rect.x = pos.x;
rect.y = pos.y;
rect.width = questionWidth;
rect.height = questionHeight;
questionRect = rect;
rect.x += questionWidth;
rect.width = answerWidth;
for (var i = 0; i < answers.Length; i++)
{
answerRects[i] = rect;
rect.x += answerWidth;
}
rect.width = correctWidth;
correctRect = rect;
}
function DisplayQuestion()
{
GUI.Label(questionRect, questionText);
for (var i = 0; i < answers.Length; i++)
{
GUI.Label(answerRects[i], displayAnswers[i]);
}
if (selectedAnswer != -1)
{
GUI.Label(correctRect, correctAnswer);
}
}
function CheckForClick(pos : Vector2) {
for (var i = 0; i < answers.Length; i++) {
if (answerRects[i].Contains(pos)) {
selectedAnswer = i;
break;
}
}
}
}
function Start ()
{
arrayOfQuestions = new Question [5];
arrayOfQuestions[0] = new Question(Vector2(30,0), "How many league titles have Liverpool Won?", "18", "5", "10", "9",0);
arrayOfQuestions[1] = new Question(Vector2(30,30), "Who won the World Cup in 2010?", "Germany", "Italy", "Brazil", "Spain", 3);
arrayOfQuestions[2] = new Question(Vector2(30,70), "Who is the Manager of Manchester United?", "David Moyes", "Rafa Benitez", "Sir Alex Fergerson", "Brendan Rodgers", 0);
arrayOfQuestions[3] = new Question(Vector2(30,105), "The Bundesliga is a professional association football league in which country?", "Italy", "Germany", "Norway", "Austria", 1);
arrayOfQuestions[4] = new Question(Vector2(30,150), "Which footballer claimed that his hand-ball goal against England in 1986 was ‘The hand of God’?","Diego Maradona ", "Pele", "Wayne Rooney", "Gary Lineker", 0);
}
function OnGUI ()
{
var e = Event.current;
if (e.type == EventType.Repaint)
{
for ( var thisQ : Question in arrayOfQuestions )
{
thisQ.DisplayQuestion();
}
}
if (e.type == EventType.MouseDown)
{
for ( var thisQ : Question in arrayOfQuestions )
{
thisQ.CheckForClick(e.mousePosition);
}
}
}
You probably want to look into:
Random.Range
In the scripting docs. $$anonymous$$eep in $$anonymous$$d that the higher number is exclusive, so you'll want to have the max range at 1 higher than your array length
Seems like more of a general program$$anonymous$$g question. But, if you want no repeats, look at "shuffling" the big Question array.
im fairly new and noobish at this, firstly as this is spawning all the questions straight away, im assu$$anonymous$$g thats what function start does is doing?
it just takes me time to understand what to do and to do it. again i do apologise for this.
so i did added the currentquestion array and edited the OnGUI function
unction OnGUI ()
{
var e = Event.current;// the event thats currently happening
if (e.type == EventType.Repaint)//other events are processed first before the repaint can actiavte
{
for ( var thisQ : Question in currentQuestion )
{
thisQ.DisplayQuestion();
}
}
if (e.type == EventType.$$anonymous$$ouseDown)
{
for ( var thisQ : Question in currentQuestion )
{
thisQ.CheckForClick(e.mousePosition);
}
}
}
not sure what the error "Argument is not enumerable error" @robertbu
'currentQuestion' is not an array.
function OnGUI ()
{
var e = Event.current;
if (e.type == EventType.Repaint)
{
currentQuestion.DispalyQuestion();
}
if (e.type == EventType.$$anonymous$$ouseDown)
{
currentQuestion.CheckForClick(e.mousePosition);
}
}
Answer by robertbu · Feb 05, 2014 at 06:44 PM
Create a current question variable 'var currentQuestion : Question;'.
Rewrite the OnGUI() code so that instead of displaying an array of questions, it display only the current question.
To initialize or reset your current question do:
currentQuestion = arrayOfQuestions[Random.Range(0, arrayOfQuestions.Length)];
If you don't want them to repeat, look into shuffling algorithms as mentioned by @Owen Reynolds. The concept plus sample source has been posted to this list several times.
Your answer
Follow this Question
Related Questions
Spawning different items from different prefabs 2 Answers
Multiple Targets 1 Answer
Help with spawning an object in a random location 1 Answer
Random loot - rarity 3 Answers
How to create random movement in 2D 2 Answers