Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there an equivalent of 2-dimensional arrays in Lua?

Asked by 9 years ago

I want to create an array in Lua to store questions and four answers in a quiz game. So arrays in Lua are called Tables right? I know that they can be iterated over but I've not seen one where you can have multiple 'columns' and 'rows', not just indexes and values. Is there a way to create a 2-dimensional array/table in Lua?

2 answers

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

I'm not entirely sure what you mean.

You can have regular tables, which should be sufficient for a quiz game.

local myTable = {"hi", 1, 5, true, "bye"}

You can have tables within tables, which i have always thought were kind of messy.

local myTable = {{"hi","bye"}, {1,2}, {true,false}}

If you want to be able to easily find the correct answer to any question, you could simply make the indexes correlate.

local questions = {
"2 + 2 = ?",
"5 + 5 = ?",
"red + blue = ?"
}

local answers = {
"4",
"10",
"purple"
}

This way answers[1] is the answer to questions[1]. You can find the answer to any question by simply using the same index in the opposite table.

Please feel free to ask questions, I was kind of confused.

EDIT:

The easiest way to do that would be to use instances. Although I'm hesitant to say it's impossible to do with tables, it would probably be cleaner to use instances. You could make it simply empty Models with the Name property storing the question and answer, or you could use StringValues and have the value property store the questions and answers. It would look something like this in the Explorer:

Questions
    Question1
        Answer1
        Answer2
        Answer3
        Answer4
0
Yeah my explanation wasn't the best. Essentially, is there a way to create a table with 5 "rows" like Question and Answer1,2,3,4 where an answer could be found like question[1,3] where that refers to Question 1, Answer 3? This is possible in other languages but I'm not sure about Lua MasterDaniel 320 — 9y
0
Do you mean like having question 1 have four possible answers and storing those answers in a table? Perci1 4988 — 9y
0
Yup exactly that :p MasterDaniel 320 — 9y
0
Read it now Perci1 4988 — 9y
Ad
Log in to vote
-2
Answered by 9 years ago

What your saying to me is most likely impossible.

Answer this question