Page 1 of 1

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

Posted: Tue Aug 29, 2017 11:03 am
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?

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

Posted: Tue Aug 29, 2017 11:10 am
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

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

Posted: Tue Aug 29, 2017 12:20 pm
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.

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

Posted: Tue Aug 29, 2017 12:41 pm
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

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

Posted: Tue Aug 29, 2017 12:43 pm
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

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

Posted: Tue Aug 29, 2017 11:00 pm
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.

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

Posted: Wed Aug 30, 2017 2:08 am
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...
;)

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

Posted: Wed Aug 30, 2017 2:40 am
by Sir_Silver
Yup good eye there Zorg =)