Hey guys. Pretty new to programming so forgive me if this seems overly basic in its wording. Here goes…
I was wondering if anyone had any experience using a shuffle bag for random item selection. Can anyone tell me if you can use “shufflebag” to pull questions randomly from a table? My game is structured as follows:
main.lua > mainMenu.lua > menu.lua > game.lua > categories.lua > utils.lua > questions.lua
The mainMenu is my start screen. When start is pressed it goes to menu, which is the list of categories. My questions for each category are stored in individual tables in their own lua files. Ex: Bonus.lua.
Currently I am using this to select random questions from the table.
local function shuffle(t)
local rand = math.random
for i = #t, 2, -1 do
local j = rand(i)
t[i], t[j] = t[j], t[i]
end
end
table.shuffle = shuffle
I store this in it’s own utils.lua file. In each question category I am calling it with table.shuffle (table name)
The problem is it only randomly “shuffles” the questions once. After that it’s in the same order every time unless I quit the app completely. I saw the video on the “shufflebag” https://youtu.be/WTirbM9RRLE?t=5m16s and thought I could use something like this. Can anyone tell me how I could implement this with the way my game is currently structures. I can not figure out how to make this access the questions in my table lua files and display one randomly selected question at a time.
Ultimately I would want there to be 5 randomly selected questions during each game (one at a time) from a large bucket of say 50 questions.
Here is an example of my question table
– QUESTIONS
– Cat questions for bonus table
local Bonus = {
{
question = “Test Question 1.”, – The actual question text
image = nil,
answer = 1, – answer for the question
answers = {
“This one is correct”,
“Generic Answer”,
“Generic Answer”,
“Generic Answer”,
},
},
{
question = “Test question 2.”,
image = nil,
answer = 2,
answers = {
“Generic Answer”,
“This one is correct”,
“Generic Answer”,
“Generic Answer”,
},
},
{
question = “Test question 3.”,
image = nil,
answer = 3,
answers = {
“Generic Answer”,
“Generic Answer”,
“This one is correct”,
“Generic Answer”
},
},
{
question = “Test question 4!”,
image = nil,
answer = 1,
answers = {
“Generic Answer”,
“Generic Answer”,
“Generic Answer”,
“Generic Answer”,
“Generic Answer”,
“Generic Answer”
},
},
{
question = “Test question 5.”,
image = nil,
answer = 1,
answers = {
“Generic Answer”,
“Generic Answer”,
“Generic Answer”,
},
},
{
question = “Test question 6?”,
image = nil,
answer = 2,
answers = {
“False”,
“True”,
},
},
}
table.shuffle(Bonus)
–Return it now
return Bonus
Any help or pointing me in the right direction would be greatly appreciated!
