Total insanity related to love.keyboard.isDown()

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
Mr. Strange
Party member
Posts: 101
Joined: Mon Aug 11, 2008 5:19 am

Total insanity related to love.keyboard.isDown()

Post by Mr. Strange »

Alright, I have a keypressed callback function in my love script, and inside that function I have this:

Code: Select all

		if(love.keyboard.isDown(love.key_f) then 
			print("hit f") 
		end
		
		if(mouse.blockingID > 0 and love.keyboard.isDown(love.key_d)) then
			print("hit d")
			table.remove(object, mouse.blockingID)
			mouse.blockingID = nil
		end
		
		
		if(love.keyboard.isDown(love.key_z)) then
			print("hit z")
		end
mouse.blockingID is sometimes a number, and is nil at all other times. When I press f, d, or z I expect to see the associated debug line. In the case of d, I expect to see it only if mouse.blockingID is currently assigned (in my game, that means the mouse is dragging an object)

But here is what actually happens:
Press f - get debug line. (as expected)
Press d, get nothing. (as expected)
Hold an object and press d - get debug line. (as expected)
Press z - get nothing. (??)
Hold an object and press z - get debug line. (??)

If I move the f test such that it follows the d test, then the f test behaves in the same way the z test does. If I move both the f and z tests above the d test, everything works the way I expect it to.

I believe lua has short-circuiting, so when mouse.blockingID > 0 fails I expect lua to just move on to the next chunk. But maybe it's getting hung up there somehow? This behavior makes no sense to me. I can't tell if it's a bad behavior in love.keyboard or just some aspect of lua I don't understand.

Clarity would be appreciated.

--Mr. Strange
Mr. Strange
Party member
Posts: 101
Joined: Mon Aug 11, 2008 5:19 am

Re: Total insanity related to love.keyboard.isDown()

Post by Mr. Strange »

Did another test - if mouse.blockingID is set to zero (instead of nil) then this problem doesn't occur.

So maybe nil > 0 is undefined, and lua just abandons that whole chunk, thus missing any checks after it?

--Mr. Strange
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Total insanity related to love.keyboard.isDown()

Post by rude »

As far as I know, comparing nil with a number causes an error. Weird that you don't get the LOVE error screen ... (unless you do?).

It's easy to fix like this, though:

Code: Select all

if(mouse.blockingID and mouse.blockingID > 0 and love.keyboard.isDown(love.key_d)) then
Mr. Strange
Party member
Posts: 101
Joined: Mon Aug 11, 2008 5:19 am

Re: Total insanity related to love.keyboard.isDown()

Post by Mr. Strange »

rude wrote:As far as I know, comparing nil with a number causes an error. Weird that you don't get the LOVE error screen ... (unless you do?).

It's easy to fix like this, though:

Code: Select all

if(mouse.blockingID and mouse.blockingID > 0 and love.keyboard.isDown(love.key_d)) then
Indeed, I get no error screen. Actually, I pretty much only get syntax errors, and not any other type of error. Maybe there is more functionality in love that I'm missing out on?

--Mr. Strange
surtic
Citizen
Posts: 74
Joined: Sat Jul 12, 2008 12:18 am

Re: Total insanity related to love.keyboard.isDown()

Post by surtic »

Are you sure you copied the code correctly?

This line looks suspicious:

Code: Select all

if(love.keyboard.isDown(love.key_f) then
There's a missing right bracket there (and Lua does complain about it).

The code should definitely shout after you set blockingID to nil. Could you attach the full code (instead of just a snippet)?
Post Reply

Who is online

Users browsing this forum: 101Corp and 2 guests