Require myData

Sorry if this is a noob question but I’m creating a quiz app and I created a file “myData.lua” with a table in it with some questions and answers. Now I need to randomly pick a question from the table to show it in my game.lua file. What do I need to do? I tried to insert this in my game.lua file:

local data= require(“myData”)

I also tried this (i saw this in this forum) to shuffle some 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

It’s not working, please help me.

First of all, make sure that myData.lua returns the table.
Then, you can pick a random question like this:

local randomNumber = math.random(1, #data)
local question = data[randomNumber]

1 Like

And for the above to work, you need to structure your myData.lua like this

local data = { "question1", "question2", "etc", }
return data
1 Like

see shuffle here:

1 Like

Be aware. SSK (and other libraries) have all these basic tools. So you don’t need to write them on your own unless it is to learn.

Docs: https://roaminggamer.github.io/RGDocs/pages/SSK2/
Code: https://github.com/roaminggamer/SSK2

1 Like

I will provide full code below for something working.

My Advise for you in case you need to learn is to try step by step.

First Thing you need to have a small program using a single main.lua file

in main.lua put your array or table of questions

define another table or array which holds already selected values

and each time you need to pick a new question, make sure it is not already selected

if this works, you can go to putting your table in an external lua file

and when this works you can move to putting your questions in external files like SQLLite

and finally you can put your questions on the cloud in a database, and call out a webservice to fetch those questions from the database and store them in a table

This is the code below:

math.randomseed( os.time() ) -- this line of code will get you better randomness

local myQuestions = {"Question1 ?","Question2 ?","Question3 ?","Question4 ?","Question5 ?","Question6 ?","Question7 ?","Question8 ?"}--Array Which holds your questions

local takenIds = {0}--Array which stores IDs of already shown questions

local function valTaken( givenId )--function to check if this question is already taken or not
	for i = 1, #takenIds do
		if takenIds [i] == givenId then
			return true
		end
	end
end

--here your code for showing questions ... i will simply put a timer to keep showing questions

local function pickQuestion()
       local newQuestion=math.random(1,#myQuestions )
       while valTaken(newQuestion) == true do
		newQuestion=math.random(1,#myQuestions )
	end
          table.insert(takenIds ,newQuestion)
           print(myQuestions[newQuestion])
end

timer.performWithDelay( 1000,pickQuestion,#myQuestions )--a timer that will run every second and iterate for the number of questions

If you copy paste this code to your main.lua file and run it it should print your list of questions each second randomly

when i started on Solar2d AKA Corona SDK 8 years ago, i wrote my first program using this code, it was a game which is on the play store … it was very difficult for me to do back then because i was new to the language :slight_smile:

Hope this helps.
Good Luck!
Tariq

1 Like

Wow, Thank you all for your help!