Strange issue - my function always thinks it is selecting something when im 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
dyl4n130
Prole
Posts: 24
Joined: Sat Jul 29, 2017 6:48 am

Strange issue - my function always thinks it is selecting something when im not

Post by dyl4n130 »

I am programming something that uses a basic click and place blueprints for a wall. You click in an area and it creates a "wall" were you click, restricted to a tile grid. Cool that works fine, now I want to make it so if you right click on a wall BP it goes away. Seems simple enough right? Should be but I have a strange problem.


Here is my mousepressed function:

Code: Select all

function love.mousepressed(x, y, button)
	placeWallBP(x, y) -- draw walls
	if button == 2 then -- if you right clicked
		wallIs, wallWhich = checkWallBP(x, y) -- check if there is a wall BP in the grid section you clicked
		if wallIs == 1 then -- if there is a wall, remove that wall from the arry
			table.remove(wallbpX, wallWhich)
			table.remove(wallbpY, wallWhich)
		end
	end
end
Here is the checkWallBP function:

Code: Select all

function checkWallBP(x, y)
	for i = 1, #wallbpX do -- check through each wall in the table for how many walls there are
		if coordtoGrid(wallbpX[i]) == coordtoGrid(x) and coordtoGrid(wallbpY[i]) == coordtoGrid(y) then -- if there is a wall on the X spot and a wall on the Y spot, return true and where the wall is in the array
			return 1, i
		end
	end
	return 0
end
two problems: for whatever reason no matter where I click it returns true. Regardless of there being a wall BP there it says there is. it accurately can tell me which wall it is, sorta. If I click where there isnt a wall it tells me the "wall" is wall number: (total walls + 1) no idea why. Also, it does say that there is a wall there when I click on a wall, but it doesnt remove it.

Walls are drawn like this:

Code: Select all

function drawWallBP()
	for i = 1, #wallbpX do
		love.graphics.draw(wallbpImg, wallbpX[i], wallbpY[i])
	end
end

any ideas folks?
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: Strange issue - my function always thinks it is selecting something when im not

Post by Sir_Silver »

Would you be so kind as to upload a .love file of your game, it would be much easier to debug that way.

If you aren't sure how to do that, you just have to zip up your main.lua with any other files you have and rename the .zip to .love
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Strange issue - my function always thinks it is selecting something when im not

Post by zorg »

You should share your placeWallBP function, since, you know, you call that in love.mousepressed no matter what, which might not be what you want... if you just table.insert walls into the highest index that way, while clicking the right button, then it's possible that's why checkallBP will return positive... just a hunch though.
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.
dyl4n130
Prole
Posts: 24
Joined: Sat Jul 29, 2017 6:48 am

Re: Strange issue - my function always thinks it is selecting something when im not

Post by dyl4n130 »

zorg wrote: Tue Aug 29, 2017 12:20 pm You should share your placeWallBP function, since, you know, you call that in love.mousepressed no matter what, which might not be what you want... if you just table.insert walls into the highest index that way, while clicking the right button, then it's possible that's why checkallBP will return positive... just a hunch though.

Code: Select all

function placeWallBP(x, y)
	table.insert(wallbpX, coordtoGrid(x))
	table.insert(wallbpY, coordtoGrid(y))
	table.insert(workerQueueX, coordtoGrid(x))
	table.insert(workerQueueY, coordtoGrid(y))
end
dyl4n130
Prole
Posts: 24
Joined: Sat Jul 29, 2017 6:48 am

Re: Strange issue - my function always thinks it is selecting something when im not

Post by dyl4n130 »

Sir_Silver wrote: Tue Aug 29, 2017 11:10 am Would you be so kind as to upload a .love file of your game, it would be much easier to debug that way.

If you aren't sure how to do that, you just have to zip up your main.lua with any other files you have and rename the .zip to .love
everything is super unstructured and spelled wrong just ignore that, this isnt supposed to be a serious project at all

http://www.mediafire.com/file/5fu1lcd2b ... uidler.rar
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: Strange issue - my function always thinks it is selecting something when im not

Post by Sir_Silver »

Well that wasn't exactly a .love file but it did have your code in it.

This should do it for you.

Code: Select all

function love.mousepressed(x, y, button)
	if button == 1 then
		placeWallBP(x, y) -- draw walls
	end
	
	if button == 2 then -- if you right clicked
		wallIs, wallWhich = checkWallBP(x, y) -- check if there is a wall BP in the grid section you clicked
		if wallIs == 1 then -- if there is a wall, remove that wall from the arry
			table.remove(wallbpX, wallWhich)
			table.remove(wallbpY, wallWhich)
		end
	end
end
The problem was that, although you were removing that last wall that you added, you were adding a wall every time just before removing the wall you actually wanted to.

Now, left click is place wall, right click is remove.

Worth mentioning, your system doesn't stop you from placing multiple walls on the same spot.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Strange issue - my function always thinks it is selecting something when im not

Post by zorg »

Sir_Silver wrote: Tue Aug 29, 2017 11:00 pm The problem was that, although you were removing that last wall that you added, you were adding a wall every time just before removing the wall you actually wanted to.

Now, left click is place wall, right click is remove.
zorg wrote: Tue Aug 29, 2017 12:20 pm You should share your placeWallBP function, since, you know, you call that in love.mousepressed no matter what, which might not be what you want...
;)
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.
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: Strange issue - my function always thinks it is selecting something when im not

Post by Sir_Silver »

Yup good eye there Zorg =)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 7 guests