Heyyyy,
I'm new to coding in general. i can't grasp and comprehend the concept of a lua table though.
Can someone please explain or provide a link that explains tables.
SHANKS
Tables
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Tables
Programming In Lua, Chapter 2.5 explains Tables. You should try to read the whole book (it's short and fast) to grasp all of Lua's concepts.
Re: Tables
A shorter explanation could go something like this:
Tables are a data structure. Data structures are useful whenever you want to store large amounts of stuff compactly.
Tables are a simple yet very flexible data structure that map anything to someting else. You can map numbers to stuff, which makes a table act like a simple list of things (this is what happens when you write a = {"this is a string", "this is another string", "yet another string"} ), or you can use something else as keys. The most common key, except for numbers, is strings. Because strings are so common keys, the syntax for using that is simplified. Instead of having to write a = {"x" = someValue, "y" = someOtherValue}, you can write a = {x = someValue, y = someOtherValue}.
Because of this, you can use tables sort of like objects in object oriented programming. You can let tables represent anything you want. Let's say you want rectangles in your game. You can let a table represent a rectangle. A rectangle needs an x-coordinate and a y-coordinate, as well as a width and height. So just store those values in a table! Like this
Tables are a data structure. Data structures are useful whenever you want to store large amounts of stuff compactly.
Tables are a simple yet very flexible data structure that map anything to someting else. You can map numbers to stuff, which makes a table act like a simple list of things (this is what happens when you write a = {"this is a string", "this is another string", "yet another string"} ), or you can use something else as keys. The most common key, except for numbers, is strings. Because strings are so common keys, the syntax for using that is simplified. Instead of having to write a = {"x" = someValue, "y" = someOtherValue}, you can write a = {x = someValue, y = someOtherValue}.
Because of this, you can use tables sort of like objects in object oriented programming. You can let tables represent anything you want. Let's say you want rectangles in your game. You can let a table represent a rectangle. A rectangle needs an x-coordinate and a y-coordinate, as well as a width and height. So just store those values in a table! Like this
Code: Select all
rectangle = {x = 10, y = 20, width = 30, height = 40}
--access contens of recangle, the following lines are equivalent
print(rectangle.x)
print(rectangle["x"])
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
Re: Tables
Look this code:Foxat wrote:Heyyyy,
I'm new to coding in general. i can't grasp and comprehend the concept of a lua table though.
Can someone please explain or provide a link that explains tables.
SHANKS
Code: Select all
myTable = {}
for row = 1, 10 do
myTable[row] = row
end
Code: Select all
myTable = {1,
2,
3,
4,
5,
6,
7,
8,
9,
10
}
myTable[2] = 2
Now something more complex:
Code: Select all
myTable = {}
for row = 1, 10 do
myTable[row] = {}
for col = 1, 4 do
myTable[row][col] = row .. " " .. col
end
end
Code: Select all
myTable = { {"1 1", "1 2", "1 3", "1 4"},
{"2 1", "2 2", "2 3", "2 4"},
{"3 1", "3 2", "3 3", "3 4"},
{"4 1", "4 2", "4 3", "4 4"},
{"5 1", "5 2", "5 3", "5 4"},
{"6 1", "6 2", "6 3", "6 4"},
{"7 1", "7 2", "7 3", "7 4"},
{"8 1", "8 2", "8 3", "8 4"},
{"9 1", "9 2", "9 3", "9 4"},
{"10 1", "10 2", "10 3", "10 4"},
}
myTable[3][2] = "3 2"
A good way to build tables without messing the dimensions is to use row and col variables in the for...next instead i and j, so it is more visual and less confusing.
Hope this is helpful.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Tables
I created this some time ago. It should give you a basic tour of the language. Tables are explained on page two.
https://github.com/kikito/love-tile-tut ... c-concepts
https://github.com/kikito/love-tile-tut ... c-concepts
When I write def I mean function.
Re: Tables
Thanks! I totally am grateful for all the help!!!
Who is online
Users browsing this forum: Amazon [Bot], Bing [Bot] and 12 guests