Question about 2 dimensional table.insert/remove

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Mynze
Prole
Posts: 3
Joined: Tue Mar 19, 2019 2:56 pm

Question about 2 dimensional table.insert/remove

Post 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
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: Question about 2 dimensional table.insert/remove

Post 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
Mynze
Prole
Posts: 3
Joined: Tue Mar 19, 2019 2:56 pm

Re: Question about 2 dimensional table.insert/remove

Post by Mynze »

Thank you so much!!! This worked incredibly fine and helped me a lot! :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests