Is this a bug or what am I doing wrong?

I’m working with polyominos (tetris shapes). This function keeps returning nil because my for … do loop always has nil; however, in the variable watch window I clearly see they aren’t nil. It should be returning the .x and .y from the table row. But it somehow says it’s nil.

function M.calculate_properties(polyomino_squares)
	-- Initialize min and max coordinates
	local min_x, max_x = math.huge, -math.huge
	local min_y, max_y = math.huge, -math.huge

	-- Iterate through each square to find the min and max coordinates
	for _, square in ipairs(polyomino_squares) do
		print("Square x:", square.x, "y:", square.y)  -- Debugging
		if square.x == nil or square.y == nil then
			print("Error: square coordinates are nil")
			--return nil -- Or handle the error appropriately
		end

That’s all the further it goes and where the problem is . square.x and square.y are nil. But in the variable watch window they aren’t. See screenshots of debug/variables:
image


image

image
image

It looks like polyomino_squares table has an extra level of depth to what your code expects. It’s not a table of 5 elements, it’s a table of 1 element that has 5 elements underneath it.

4 Likes

That’s what I was thinking too, but when I look at how I create the table and that I’m returning pairs, I’m not so sure. And based on debug code I can’t figure out how to get around it.

How should I get to the next table down then if not squares?

Without changing how you generate that table, in the loop code, you should be able to replace ipairs(polyomino_squares) with ipairs(polyomino_squares[1])

2 Likes

Thanks I’ll give it

Thanks that appears to get me to a new issue I can work on. :slight_smile:

But here’s the code I use to create the table. Is there a better way to do this so it’s not a table under a table?

if factory_id == "/garden_factories#broccoli_garden" then
			local id = polyomino_module.create_polyomino({{
				{x = 0, y = 0}, -- Top-left block
				{x = 1, y = 0}, -- Top-right block
				{x = 0, y = 1}, -- Middle-left block
				{x = 1, y = 1}, -- Middle-right block
				{x = 0, y = 2}  -- Bottom-left block
			}})
		elseif factory_id == "/garden_factories#carrot_garden" then
			local id = polyomino_module.create_polyomino({{
				{x = -2, y = 0}, -- Two blocks to the left of the center
				{x = -1, y = 0}, -- One block to the left of the center
				{x = 0, y = 0},  -- Central block
				{x = 1, y = 0},  -- One block to the right of the center
				{x = 2, y = 0}   -- Two blocks to the right of the center
			}})
		elseif factory_id == "/garden_factories#chilipepper_garden" then
			local id = polyomino_module.create_polyomino({{
				{x = 0, y = 0}, -- Top-left block
				{x = 1, y = 0}, -- Top-middle block
				{x = 2, y = 0}, -- Top-right block
				{x = 0, y = -1}, -- Middle-left block
				{x = 0, y = -2}  -- Bottom-left block
			}})
		elseif factory_id == "/garden_factories#greenpepper_garden" then
			local id = polyomino_module.create_polyomino({{
				{x = -1, y = 1}, -- Top-left block
				{x = -1, y = 0}, -- Middle-left block
				{x = 0, y = 0},  -- Center block (reference point)
				{x = 1, y = 0},  -- Middle-right block
				{x = 1, y = -1}  -- Bottom-middle block
			}})
		elseif factory_id == "/garden_factories#onion_garden" then
			local id = polyomino_module.create_polyomino({{
				{x = -1, y = 0}, -- Top-left block
				{x = 0, y = 0},  -- Top-middle block (origin)
				{x = 1, y = 0},  -- Top-right block
				{x = 0, y = -1}  -- Middle block of the second row
			}})
		elseif factory_id == "/garden_factories#radish_garden" then
			local id = polyomino_module.create_polyomino({{
				{x = -1, y = 1}, -- Block above and to the left of the center
				{x = 0, y = 1},  -- Block above the center
				{x = -1, y = 0}, -- Block to the left of the center
				{x = 0, y = 0},  -- Center block (origin)
				{x = 1, y = 0}   -- Block to the right of the center
			}})
		end

At the top and bottom lines you have two sets of curly braces which creates an extra nested table.

1 Like

Ah, thank you. I didn’t think that mattered. But I see clearly it does. I was beating my head against the wall with this now I have it fixed upon creation of the table. Thank you so much.

2 Likes