Tables drive me nuts sometimes: how is this wrong?

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
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Tables drive me nuts sometimes: how is this wrong?

Post by togFox »

Code: Select all

local map = {
    {0,1,0,1,0},
    {0,1,0,1,0},
    {0,1,1,1,0},
    {0,0,0,0,0},
}

for i,_ in ipairs(map) do
	for j, _ in ipairs(map) do
		io.write(map[i][j] .. " ")
	end
	io.write('\n')
end
io.write('\n')
Pretty straight forward? Console says:

Code: Select all

0 1 0 1
0 1 0 1
0 1 1 1
0 0 0 0

Where did the last column go? :(
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Tables drive me nuts sometimes: how is this wrong?

Post by togFox »

I clearly don't know what I'm doing. Here is another example of something very fundamentally wrong with my thinking:

Code: Select all

	local objMap = {}

	for y = 1,10 do
		objMap[y] = {}
	end
	
	-- instantiate the map with default values
	for y = 1,10 do
		for x = 1,8 do
			print("Instantiating " .. x .. "," .. y)
			objMap[x][y] = {}
		end
	end	
Works as expected:

Code: Select all

Instantiating 1,1
Instantiating 2,1
Instantiating 3,1
[snip]
Instantiating 7,1
Instantiating 8,1
Instantiating 1,2
Instantiating 2,2
[snip]
Instantiating 6,10
Instantiating 7,10
Instantiating 8,10
So I have an 8 x 10 grid? Yes?

Code: Select all

	print("Max y = " .. table.maxn(objMap) .. " max x = " .. table.maxn(objMap[1]))
No - seems not:

Code: Select all

Max y = 10 max x = 10
How?

Going to do some netflix and re-evaluate my life choices.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Tables drive me nuts sometimes: how is this wrong?

Post by darkfrei »

Code: Select all

for i, line in ipairs(map) do
	for j, value in ipairs(line) do
		io.write(value .. " ")
	end
	io.write('\n')
end
Or

Code: Select all

for i = 1, #map do --#map is 4
	local line = map[i]
	for j = 1, #line do -- #line is 5
		local value = map[i][j]
		io.write(value .. " ")
	end
	io.write('\n')
end
Where line = map[1] was your first line {0,1,0,1,0}.

Next, map 10x8:

Code: Select all

local map = {}
for y = 1, 8 do -- remember, lines!
  map[y] = {}
  for x = 1, 10 do
    print("Instantiating " .. x .. "," .. y)
    map[y][x] = {} -- lines are first!
  end
end
Or

Code: Select all

local map = {}
for y = 1, 8 do
  for x = 1, 10 do
    map[x] = map[x] or {} -- if no map[x], then create the empty one
    print("Instantiating " .. x .. "," .. y)
    map[x][y] = {}
  end
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Tables drive me nuts sometimes: how is this wrong?

Post by togFox »

I think I'm messing up my y's and x's. I'll take a break and clear my head and start again.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
User avatar
bigbruhh0
Prole
Posts: 17
Joined: Fri Mar 26, 2021 10:36 pm
Location: Russia
Contact:

Re: Tables drive me nuts sometimes: how is this wrong?

Post by bigbruhh0 »

togFox wrote: Sat Apr 10, 2021 3:41 am

Code: Select all

local map = {
    {0,1,0,1,0},
    {0,1,0,1,0},
    {0,1,1,1,0},
    {0,0,0,0,0},
}

for i,_ in ipairs(map) do
	for j, _ in ipairs(map) do
		io.write(map[i][j] .. " ")
	end
	io.write('\n')
end
io.write('\n')
Pretty straight forward? Console says:

Code: Select all

0 1 0 1
0 1 0 1
0 1 1 1
0 0 0 0

Where did the last column go? :(
its just because your second cycle is incorrect
you have 4 sections in your "map" table : map[1],map[2],map[3],map[4]
and you are passing through i ={1,2,3,4}, j={1,2,3,4}
so, because of this you are messing 5th column
you should write
for j=1,5
or
for j=1,#map
( if it will work, I'm not sure about second option xD)
or just like this
for j, _ in ipairs(map[ i ]) do
in love with love2d
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Tables drive me nuts sometimes: how is this wrong?

Post by togFox »

Thanks everyone. I stopped calling things x and y because it was mentally screwing me up. I started calling things rows and cols and then everything fell into place instantly.

I'm not sure if this link works but this is a random tiled map with JUMPER correctly pathfinding to the cell (1,1) in the top left corner.

[The red scribble is my leet photoshop skills for demo purposes]

Image

Image
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests