Page 1 of 1

Question about 2 dimensional table.insert/remove

Posted: Sun Nov 24, 2019 1:46 pm
by Mynze
I'm having huge troubles with tables right now. I'm currently making an endless runner where each value of a table represents a tile. I thought that I could endlessly add values/tiles in a row from a table while also removing the first one. Everything besides that works perfectly fine. :(

This is an example code. So I've first created a table with 3 rows:

Code: Select all

map = {
  {1, 1, 1, 1},
  {0, 0, 0, 0},
  {2, 3, 2, 3}
}
Now I want to extend the third row, by the value of 5, for e.g.:

Code: Select all

table.insert(map, 3,{5})
This only adds another row, between the already existing second and third row.

How do I use table.insert and table.remove for a 2 dimensional table
and/or is there a way to move the first column to the last place and changing the values ?

Thanks in advance and sorry for my bad English!! :D

Re: Question about 2 dimensional table.insert/remove

Posted: Sun Nov 24, 2019 2:11 pm
by Andlac028
Hello,
You have a table, which have another 3 tables, that is equal to:

Code: Select all

map={}
map[1]={1,1,1,1}
map[2]={0,0,0,0}
map[3]={2,3,2,3}
--or
map={[1]={1,1,1,1},[2]={0,0,0,0},[3]={2,3,2,3}}
So if you want to edit map[3] (third row of table map) then you must do:

Code: Select all

table.insert(map[3],5)
--or
map[3][#map[3]+1]=5 -- #map[3] is length of table map[3]
That you can do for all table functions

Re: Question about 2 dimensional table.insert/remove

Posted: Sun Nov 24, 2019 2:18 pm
by Mynze
Thank you so much!!! This worked incredibly fine and helped me a lot! :)