Far as I see, the questions array is being created locally inside a function and hasn't been declared global. You're also calling that particular function in a loop, which makes no sense. Every time you try to find a new question, you're recreating the array over and over. Also, I don't see where you ever define iCurrentQuestionId
Have one array containing your questions. Have another array contain a pool of available questions that can be asked. This pool is just a list of indices to all the questions in the questions array. As you grab a question from the pool, remove it. Think of a deck of cards, you'll pulling a card from the deck. This would be more efficient that looping through the array trying to find one that isn't a duplicate. Essentially, it's the opposite of what you did. In your case, you added questions to the pool as you asked them, then searched each time to make sure a question wasn't already added to the pool. In my example, we throw all the questions into the pool from the start then randomly extract them, shrinking the pool each time.
Try this example:
type myQuestionType
question as string
answer1 as string
answer2 as string
answer3 as string
answer4 as string
image as string
endtype
questions as myQuestionType[]
// load questions into array
loadQuestions(questions)
// This will contain the available questions left to ask
dim questionsAvailable[questions.length] as integer // have to use dim to define an array with a dynamic size
// each index pertains to a question in questions[] array
for i = 0 to questionsAvailable.length
questionsAvailable[i] = i
next i
currentQuestionId = -1
do
// Press space key to see next random question
if getRawKeyPressed(32) = 1 and getRemainingQuestions() > 0
currentQuestionId = getRandomQuestion()
endif
print("Questions Remaining: " + str(getRemainingQuestions()))
print("")
if currentQuestionId > -1
print("Q: "+questions[currentQuestionId].question)
print(" A. "+questions[currentQuestionId].answer1)
print(" B: "+questions[currentQuestionId].answer2)
print(" C: "+questions[currentQuestionId].answer3)
print(" D: "+questions[currentQuestionId].answer4)
else
print("Press spacebar to grab a random question")
endif
sync()
loop
// Grabs a random question from the available choices and removes it so it can't be selected again
function getRandomQuestion()
i = random(0, questionsAvailable.length)
q = questionsAvailable[i]
questionsAvailable.remove(i)
endfunction q
// Returns how many questions are left
function getRemainingQuestions()
count = questionsAvailable.length + 1
endfunction count
function loadQuestions(arr ref as myQuestionType[])
question as myQuestionType
question.question = "Is this question number 1?"
question.image = "qimage1.jpg"
question.answer1 = "a"
question.answer2 = "b"
question.answer3 = "c"
question.answer4 = "d"
arr.insert(question)
question.question = "Can this question be 2?"
question.image = "qimage2.jpg"
question.answer1 = "1"
question.answer2 = "2"
question.answer3 = "3"
question.answer4 = "4"
arr.insert(question)
question.question = "What if we make this the 3rd question?"
question.image = "qimage3.jpg"
question.answer1 = "1a"
question.answer2 = "2b"
question.answer3 = "3c"
question.answer4 = "4d"
arr.insert(question)
endfunction