Daw588
(Daw588)
October 29, 2022, 7:10am
#21
You can extend my Util function to return the named index (key).
function Utils.getEntryByIndex(entries, index)
local pos = 0
for key, entry in entries do
pos += 1
if pos == index then
return entry, key
end
end
end
And then in another script:
local randomQuestionIndex = math.random(1, Utils.getCountOf(questions))
local choices, question = Utils.getEntryByIndex(questions, randomQuestionIndex)
Utils.shuffle(choices) -- Randomize order of choices
print(question, choices)
1 Like
rfisty
(rfisty)
October 30, 2022, 5:54pm
#22
How can I get it from printing out like this?
Daw588
(Daw588)
October 30, 2022, 6:09pm
#23
Turn off “Show Memory Address for Tables” in the output window .
Daw588
(Daw588)
October 30, 2022, 6:29pm
#25
Did you try turning off “Log Mode” as well?
rfisty
(rfisty)
October 30, 2022, 6:35pm
#26
How can I retrieve all the choices in text format? Right now print(choices[1])
prints out as nil
Daw588
(Daw588)
October 30, 2022, 6:51pm
#27
Use Utils.getEntryByIndex()
to retrieve the entry by numerical index, this is because you are trying to get 1
from this:
{
["55"] = false,
["42"] = false,
["66"] = true,
["33"] = false,
}
But 1
doesn’t exist in here, only "55"
, "42"
, "66"
, and "33"
. Therefore:
local choice = Utils.getEntryByIndex(choices, 1)
I would strongly recommend learning basics of how tables work in Lua.
rfisty
(rfisty)
October 30, 2022, 6:54pm
#28
This prints out true or false, I need it to print out the number/string answer
Yes, I will certainly try once I have the time to do so
Daw588
(Daw588)
October 30, 2022, 6:56pm
#29
Sorry, I forgot that it returns the value. You can get the second returned value which is the key.
local isCorrect, choice = Utils.getEntryByIndex(choices, 1)
1 Like
system
(system)
Closed
November 13, 2022, 6:56pm
#30
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.