Table sometimes nil.. sometimes not?

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
Xgor
Prole
Posts: 2
Joined: Mon May 15, 2017 9:29 pm

Table sometimes nil.. sometimes not?

Post by Xgor »

(If you just want to look at the source and figure it out from there it's here https://github.com/Xgor/Lovetris )

Hello Löve forum. I'm currently trying to do a tetris clone in Löve to learn how Löve and Lua works.
I was currently trying to get the different parts of the tetromino so i saved it into a 16 size table that shows the value.

Code: Select all

iTetromino= {0,1,0,0,
			0,1,0,0,
			0,1,0,0,
			0,1,0,0}
And then I create the Tetromino here and place the block pattern in tetromino.block (or t.block here)

Code: Select all

function getNewTetromino()
	local t ={}

	t.xPos =PLAYFIELD_WIDTH/2
	t.yPos = 0

	local blockType = love.math.random(7)
 	t.block = zTetromino
	if blockType == 1 then t.block = iTetromino 
	elseif blockType == 2 then t.block = oTetromino 
	elseif blockType == 3 then t.block = tTetromino 
	elseif blockType == 4 then t.block = jTetromino 
	elseif blockType == 5 then t.block = lTetromino 
	elseif blockType == 6 then t.block = sTetromino 
	else t.block = zTetromino end
 

	return t
end
V The problem however is THIS code V

Code: Select all

if tetromino.block[x+y*4] > 0 then
For some reason it keeps insisting that it's nil and I can't figure for the life of me why it is so.


To make the game not crash I had to add so it won't run the code if there is a problem but it never runs then.

Code: Select all

not tetromino.block[x+y*4] == nil

I know that it might be vauge so here are the examples when it doesn't work at all.

Code: Select all

function fallUpdate(tetromino)
	tetromino.yPos = tetromino.yPos +1
	
	TetrominoFallTimer = FALL_SPEED

	if checkTetrominoCollision(tetromino,tetromino.xPos,tetromino.yPos+1,g_playfield) then
		for x = 0,3 do
			for y = 0,3 do
				if not tetromino.block[x+y*4] == nil and tetromino.block[x+y*4] > 0 then
					playfield.blocks[tetromino.xPos+x][tetromino.yPos+y] = tetromino.block[x+y*4]
				end
			end
		end
		getNextTetromino()
	end

end

Code: Select all

function checkTetrominoCollision(tetromino,xOffset,yOffset,playfield)


	if xOffset <0 or xOffset>7 or yOffset>17 then
		return true
	end

	for x = 0,3 do
		for y = 0,3 do
			if not tetromino.block[x+y*4] == nil and tetromino.block[x+y*4] > 0 then
				if 	playfield.blocks[xOffset+x][yOffset+y] > 0 then
					return true
				end
			end
		end
	end
	return false

end
The thing that confuses me is that that I do use tetromino.block[x+y*4] later in the code and it works just as intented so I'm confused what's wrong.

Code: Select all

function drawTetromino(xOffset,yOffset,tetromino)
	for x = 0,3 do
		for y = 0,3 do
	--		love.graphics.print("Test", x, y)
	--		drawBlock(2,2,2)
			drawBlock(tetromino.block[x+y*4],xOffset+ math.floor(x+tetromino.xPos)*BLOCK_WIDTH,
				yOffset+math.floor(y+tetromino.yPos)*BLOCK_HEIGHT)
		end
	end
end
Hope you people are ok with wall of code and can help me :)
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Table sometimes nil.. sometimes not?

Post by zorg »

Hi and welcome to the forums!

Your indices when iterating are wrong, lua indices are (by default) 1-based, not 0-based, so your code is faulty as it is now.

The very first code snippet defined the table members from the integer key 1 to 16, not 0 to 15.
Your last two uses for loops that iterate from 0 to 3, instead of from 1 to 4.

You can fix it both ways, since you're not using # or ipairs, only the plain for loop... either define the first element as [0] = something, then it'll go from 1 to 15 by itself naturally, or fix your indices in the iterators. If you do choose to go with 1-based indices, keep in mind that modular arithmetic will get a bit wonky, as will your x+y*4 stuff, since you'll need to subtract 1 from both variables every time...maybe.

Code: Select all

iTetromino= {[0] = 0,1,0,0,
			0,1,0,0,
			0,1,0,0,
			0,1,0,0}
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Xgor
Prole
Posts: 2
Joined: Mon May 15, 2017 9:29 pm

Re: Table sometimes nil.. sometimes not?

Post by Xgor »

Thank you so much. So used to the way arrays works in other languages so i didn't think that index works different in lua. I added +1 to all the x+*4 stuff but both solutions seem to work great.
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests