Initialise manually a table with 2 or 3 dimensions.

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Kitsune64
Prole
Posts: 9
Joined: Mon Jul 12, 2021 3:33 pm

Initialise manually a table with 2 or 3 dimensions.

Post by Kitsune64 »

Hi,

I have a trouble about initialise a table with 2 dimensions...How can I do it manually?

My code:

Code: Select all

	room[1][1]="Tappez commencer ou quitter"
	room[1][2]="alice.png"
	room[2][1]="Il était une fois dans un autre monde..."
	room[2][2]="start.png"
That make an error about room and nil value.

Maybe I will make more dimensions after but for now I want to try like this.

Thanks
User avatar
GVovkiv
Party member
Posts: 686
Joined: Fri Jan 15, 2021 7:29 am

Re: Initialise manually a table with 2 or 3 dimensions.

Post by GVovkiv »

Code: Select all

room = {
	{
	"Tappez commencer ou quitter",
	"alice.png"
	},
	{
	"Il était une fois dans un autre monde...",
	"start.png"
	},
}
or

Code: Select all

room = {
	[1] = {
    [1] = "Tappez commencer ou quitter",
    [2] = "alice.png"
	},
	[2] = {
    [1] = "Il était une fois dans un autre monde...",
    [2] = "start.png"
	},
}
or

Code: Select all

room = {}
room[1] = {}
	room[1][1] = "Tappez commencer ou quitter"
	room[1][2] = "alice.png"
room[2] = {}
	room[2][1] = "Il était une fois dans un autre monde..."
	room[2][2] = "start.png"
You mean that?
Last edited by GVovkiv on Mon Jul 12, 2021 4:56 pm, edited 1 time in total.
Kitsune64
Prole
Posts: 9
Joined: Mon Jul 12, 2021 3:33 pm

Re: Initialise manually a table with 2 or 3 dimensions.

Post by Kitsune64 »

Ok I found a solution I must initialize room[1] and room[2] before initialize the others

Code: Select all

room[1]={}
room{2}={}
Don't really know why..
Kitsune64
Prole
Posts: 9
Joined: Mon Jul 12, 2021 3:33 pm

Re: Initialise manually a table with 2 or 3 dimensions.

Post by Kitsune64 »

Ho yes your solution work! Thanks!
User avatar
GVovkiv
Party member
Posts: 686
Joined: Fri Jan 15, 2021 7:29 am

Re: Initialise manually a table with 2 or 3 dimensions.

Post by GVovkiv »

Kitsune64 wrote: Mon Jul 12, 2021 4:54 pm Ok I found a solution I must initialize room[1] and room[2] before initialize the others

Code: Select all

room[1]={}
room{2}={}
Don't really know why..
Maybe because before putting values inside table, well, you should create table on first place?
Kitsune64
Prole
Posts: 9
Joined: Mon Jul 12, 2021 3:33 pm

Re: Initialise manually a table with 2 or 3 dimensions.

Post by Kitsune64 »

Maybe, or it sure...Now that work well. Thanks.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Initialise manually a table with 2 or 3 dimensions.

Post by Jasoco »

If you know how big you're gonna want the table to be you can create an empty one easily first:

Code: Select all

my_table = {}
for x = 1, width_of_table do
  my_table[x] = {}
  for y = 1, height_of_table do
    my_table[x][y] = false 
    -- Or whatever other value you want here. I used a boolean for ease of checking later
    -- but you can use an empty string "" or a number 0 or whatever you want to denote that the cell is "empty"
  end
end
Then you'll be able to just replace any table cell in the grid at any time with whatever you want like you do in the OP.

You can keep nesting tables if you want too. However deep you need to go.
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Initialise manually a table with 2 or 3 dimensions.

Post by darkfrei »

Jasoco wrote: Tue Jul 27, 2021 6:44 pm If you know how big you're gonna want the table to be you can create an empty one easily first:

Code: Select all

my_table = {}
for x = 1, width_of_table do
  my_table[x] = {}
  for y = 1, height_of_table do
    my_table[x][y] = false 
    -- Or whatever other value you want here. I used a boolean for ease of checking later
    -- but you can use an empty string "" or a number 0 or whatever you want to denote that the cell is "empty"
  end
end
Then you'll be able to just replace any table cell in the grid at any time with whatever you want like you do in the OP.

You can keep nesting tables if you want too. However deep you need to go.
And in other direction:

Code: Select all

map ={}
for x=1,10 do
  for y = 1, 10 do
    map[y] = map[y] or {}
    map[y][x] = math.random()
  end
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests