How to reload scene to shuffle questions?

Everytime i relaunch my game the questions always shuffle but when in-game it doesn’t (mainMenu.lua --> questions.lua --> gameOver.lua --> mainMenu.lua) 

local M = { { question = "9 + 8", image = nil, answer = 2, answers = { "11", "17", "12", }, }, { question = "7 + 6", image = nil, answer = 3, answers = { "10", "12", "13", }, }, { question = "8 + 8", image = nil, answer = 2, answers = { "15", "16", "17", }, }, { question = "9 + 9", image = nil, answer = 3, answers = { "17", "19", "18", }, }, { question = "7 + 5", image = nil, answer = 1, answers = { "12", "16", "13", }, }, { question = "8 + 2", image = nil, answer = 3, answers = { "12", "14", "10", }, }, { question = "6 + 2", image = nil, answer = 2, answers = { "9", "8", "5", }, }, { question = "4 + 3", image = nil, answer = 2, answers = { "8", "7", "9", }, }, { question = "8 + 4", image = nil, answer = 3, answers = { "13", "11", "12", }, }, { question = "7 + 7", image = nil, answer = 1, answers = { "14", "10", "16", }, }, } 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 shuffle(M) return M

Lua modules are only loaded once. This means your shuffle function is only called once. You essentially have two options here. Make the shuffle function available on the M object or unload the module.

I would opt for the first option because it makes it easier to deal with in code and introduces less risk into your application.

Declare the shuffle function like this, call it from somewhere in your game whenever shuffling is required and remove line 119…

function M: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

Instead of removing line 119 you could also call shuffle the first time the module is loaded by changing it like this…

M:shuffle(M)

As an addition to the comment above but posted separately to indicate a new piece of advice, I would actually move your shuffle function into a utility library and dissociate it from the actual table of content being shuffled.

Why?

Because the very generic operation of shuffling the contents of a table object is not specific to your table of questions and answers. It makes sense to move it to a library where it is more fitting in purpose and can be used for shuffling other tables.

Where?

I would recommend creating a utils.lua and making the shuffle function available on the table.* library. Your function then, effectively, becomes an extension to the Corona table API and thus available to your whole application without serious performance impediment.

How?

Like this…

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

Within your listing above you would then call the new table.shuffle() function like this…

table.shuffle(M)

Hey, I am having the exact same issue with the exact same template/app layout. Did you ever get this working? Any info you could pass along would be helpful! Thanks. 

Lua modules are only loaded once. This means your shuffle function is only called once. You essentially have two options here. Make the shuffle function available on the M object or unload the module.

I would opt for the first option because it makes it easier to deal with in code and introduces less risk into your application.

Declare the shuffle function like this, call it from somewhere in your game whenever shuffling is required and remove line 119…

function M: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

Instead of removing line 119 you could also call shuffle the first time the module is loaded by changing it like this…

M:shuffle(M)

As an addition to the comment above but posted separately to indicate a new piece of advice, I would actually move your shuffle function into a utility library and dissociate it from the actual table of content being shuffled.

Why?

Because the very generic operation of shuffling the contents of a table object is not specific to your table of questions and answers. It makes sense to move it to a library where it is more fitting in purpose and can be used for shuffling other tables.

Where?

I would recommend creating a utils.lua and making the shuffle function available on the table.* library. Your function then, effectively, becomes an extension to the Corona table API and thus available to your whole application without serious performance impediment.

How?

Like this…

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

Within your listing above you would then call the new table.shuffle() function like this…

table.shuffle(M)

Hey, I am having the exact same issue with the exact same template/app layout. Did you ever get this working? Any info you could pass along would be helpful! Thanks.