Collision (Need Help)

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.
TheScriptan
Citizen
Posts: 56
Joined: Wed Feb 27, 2013 7:53 pm

Collision (Need Help)

Post by TheScriptan »

I've been working with simple rectangle collision, and got an error: "attempt to index global 'rectangle' (a nil value) '".
What does it means? Btw here is the code:

Code: Select all



function game_load()
	rectangle = {
		{x = 0, y = 0, w = 128, h = 128},
		{x = 400, y = 300, w = 128, h = 128}
	}
end

function game_update(dt)
	player_update(dt)
	
	if player.x > love.graphics.getWidth() / 2 then
		camera.x = player.x - love.graphics.getWidth() / 2
	end
	if player.y > love.graphics.getWidth() / 2 then
		camera.y = player.y - love.graphics.getWidth() / 2
	end
end

function game_draw()
	camera:set()
	if CheckCollision(player.x, player.y, player.w, player.h, rectangle[2].x, rectangle[2].y, rectangle[2].w, rectangle[2].h) then
		love.graphics.setColor(255,0,0)
	else
	love.graphics.setColor(255,255,255)
	end
	for i=1, #rectangle do
		love.graphics.rectangle("fill",rect[i].x,rectangle[i].y,rectangle[i].w,rectangle[i].h)
	end
	love.graphics.print("FPS: ".. love.timer.getFPS(),camera.x+16,camera.y+16)
	player_draw()
	
	camera:unset()
end

function CheckCollision(x1, y1, w1, h1, x2, y2, w2, h2)
	if x1 > (x2 + w2) or (x1 + w1) < x2 then return false end
	if y1 > (y2 + h2) or (y1 + h1) < y2 then return false end
	return true
end
User avatar
Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

Re: Collision (Need Help)

Post by Hexenhammer »

TheScriptan wrote:
for i=1, #rectangle do
love.graphics.rectangle("fill",rect.x,rectangle.y,rectangle.w,rectangle.h)
end


I think you mean "rectangle" here, right?
TheScriptan
Citizen
Posts: 56
Joined: Wed Feb 27, 2013 7:53 pm

Re: Collision (Need Help)

Post by TheScriptan »

Umm, yeah, but the error goes to 23 line, which is

Code: Select all

if CheckCollision bla bla bla
User avatar
Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

Re: Collision (Need Help)

Post by Hexenhammer »

"attempt to index global 'rectangle' (a nil value) '"
That just means you try to use an object called "rectanlge" but no such object exists. "A nil value" = does not exist in Luaspeak. It says "global" because all objects are global by default in Lua i.e. they can be used everywhere. According to that logic non-existing objects are "global" too.. in a way. And yes that error message certainly wasn't written to be newbie friendly.
User avatar
Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

Re: Collision (Need Help)

Post by Hexenhammer »

TheScriptan wrote:Umm, yeah, but the error goes to 23 line, which is

Code: Select all

if CheckCollision bla bla bla
I don't see any other obvious problems. Post the entire source (i.e. something people can actually run) then I could easily tell you.
TheScriptan
Citizen
Posts: 56
Joined: Wed Feb 27, 2013 7:53 pm

Re: Collision (Need Help)

Post by TheScriptan »

Hexenhammer wrote:
"attempt to index global 'rectangle' (a nil value) '"
"rectanlge"
But there is no words like rectanlge, i can't find it. :brows:
User avatar
Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

Re: Collision (Need Help)

Post by Hexenhammer »

TheScriptan wrote:
Hexenhammer wrote:
"attempt to index global 'rectangle' (a nil value) '"
"rectanlge"
But there is no words like rectanlge, i can't find it. :brows:
:? I did not write that. I said you wrote "rect" instead of "rectangle", and that causes an error because there is no object called "rect" but there is an object called "rectangle".. all over your code.
TheScriptan
Citizen
Posts: 56
Joined: Wed Feb 27, 2013 7:53 pm

Re: Collision (Need Help)

Post by TheScriptan »

Code looks now like this:

Code: Select all



function game_load()
	rectangle = {
		{x = 0, y = 0, w = 128, h = 128},
		{x = 400, y = 300, w = 128, h = 128}
	}
end

function game_update(dt)
	player_update(dt)
	
	if player.x > love.graphics.getWidth() / 2 then
		camera.x = player.x - love.graphics.getWidth() / 2
	end
	if player.y > love.graphics.getWidth() / 2 then
		camera.y = player.y - love.graphics.getWidth() / 2
	end
end

function game_draw()
	camera:set()
	if CheckCollision(player.x, player.y, player.w, player.h, rectangle[2].x, rectangle[2].y, rectangle[2].w, rectangle[2].h) then
		love.graphics.setColor(255,0,0)
	else
	love.graphics.setColor(255,255,255)
	end
	for i=1, #rectangle do
		love.graphics.rectangle("fill",rectangle[i].x,rectangle[i].y,rectangle[i].w,rectangle[i].h)
	end
	love.graphics.print("FPS: ".. love.timer.getFPS(),camera.x+16,camera.y+16)
	player_draw()
	
	camera:unset()
end

function CheckCollision(x1, y1, w1, h1, x2, y2, w2, h2)
	if x1 > (x2 + w2) or (x1 + w1) < x2 then return false end
	if y1 > (y2 + h2) or (y1 + h1) < y2 then return false end
	return true
end
Same error :|
User avatar
Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

Re: Collision (Need Help)

Post by Hexenhammer »

Again, post the entire source. I don't get any errors in the parts of the source I can run. I guess you are destroying the "rectangle" object somewhere.. somewhere in the parts of the code you did not post.
TheScriptan
Citizen
Posts: 56
Joined: Wed Feb 27, 2013 7:53 pm

Re: Collision (Need Help)

Post by TheScriptan »

Here is .love file, maybe you can find out what is wrong with if, because still can't find it, i used rectangle object only in game.lua :(
Attachments
Invaders Must Die.love
Crappy Name
(2.58 KiB) Downloaded 241 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 32 guests