love2d thinks the number "3" is a bool

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
fixylol
Prole
Posts: 21
Joined: Thu Feb 25, 2016 2:16 am

love2d thinks the number "3" is a bool

Post by fixylol » Mon Apr 11, 2016 9:29 pm

i have a function that calculates the position of an enemy in tiles (32x32 squares). i have the starting position of the enemies stored in a table. for some reason, when love.graphics.draw tries to draw the enemy onto the screen, it thinks the first value of the table (which by the way is 3) is a bool value, and i cant figure out why. there isnt a single bool value within the entire level.

here's a love file in case you don't understand: (press P to advance to the next level)
awedd-beta.love
(6.1 MiB) Downloaded 77 times
Edit: nevermind, i was dumb
Last edited by fixylol on Wed Apr 13, 2016 5:57 pm, edited 1 time in total.

User avatar
DaedalusYoung
Party member
Posts: 402
Joined: Sun Jul 14, 2013 8:04 pm

Re: love2d thinks the number "3" is a bool

Post by DaedalusYoung » Mon Apr 11, 2016 10:06 pm

This:

Code: Select all

v[3] == 2 and v[2] + 1 or v[3] == 4 and v[2] - 1
seems to return false.

fixylol
Prole
Posts: 21
Joined: Thu Feb 25, 2016 2:16 am

Re: love2d thinks the number "3" is a bool

Post by fixylol » Mon Apr 11, 2016 10:07 pm

DaedalusYoung wrote:This:

Code: Select all

v[3] == 2 and v[2] + 1 or v[3] == 4 and v[2] - 1
seems to return false.
it'shouldnt set the value to false though :/

User avatar
TheOdyssey
Citizen
Posts: 52
Joined: Mon Jul 13, 2015 4:29 pm
Location: Turn Around...

Re: love2d thinks the number "3" is a bool

Post by TheOdyssey » Tue Apr 12, 2016 1:04 am

That does generates a boolean

Code: Select all

 v[2] = v[3] == 2 
Is the exact same thing as

Code: Select all

if v[3] == 2 then
     v[2] = true
end

User avatar
zorg
Party member
Posts: 2993
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: love2d thinks the number "3" is a bool

Post by zorg » Tue Apr 12, 2016 1:55 am

Probably some precedence issues;

Code: Select all

-- v[3] == 2 and v[2] + 1 or v[3] == 4 and v[2] - 1 should be
something = ((v[3] == 2) and (v[2] + 1)) or ((v[3] == 4) and (v[2] - 1))
...well, technically, you could only enclose the whole clause after the or, but this is a bit more straightforward.
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
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: love2d thinks the number "3" is a bool

Post by airstruck » Tue Apr 12, 2016 7:21 am

Adding those parens won't change precedence; "==" > "and" > "or" (see PIL 3.5) so "A == B and C or D == E and F" is equivalent to "((A == B) and C) or ((D == E) and F)" ... if A ~= B and D ~= E then the expression does evaluate to false, though (noted earlier).

User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: love2d thinks the number "3" is a bool

Post by vrld » Tue Apr 12, 2016 8:31 am

Code: Select all

x = v[3] == 2 and v[2] + 1 or v[3] == 4 and v[2] - 1
is equivalent to:

Code: Select all

if v[3] == 2 then
    x = v[2] + 1
elseif v[3] == 4 then
    x = v[2] - 1
else
    x = false
end
Are you sure v[3] is always 2 or 4?
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine

User avatar
DPlayer234
Prole
Posts: 4
Joined: Tue Apr 12, 2016 3:36 pm
Contact:

Re: love2d thinks the number "3" is a bool

Post by DPlayer234 » Tue Apr 12, 2016 3:43 pm

The solution is pretty simple.
This: (lines 225-228)

Code: Select all

for i,v in pairs(CurrentLevel.Enemies) do
    v[1] = v[3] == 1 and v[1] + 1 or v[3] == 3 and v[1] - 1
    v[2] = v[3] == 2 and v[2] + 1 or v[3] == 4 and v[2] - 1
end
would set v[1] to false if neither "v[3] == 1" or "v[3] == 3" return true. (Same for v[2])

Why? In case no statement in such a "chain" is true, it will return false.

What I did to prevent the crash was simple:

Code: Select all

for i,v in pairs(CurrentLevel.Enemies) do
    v[1] = v[3] == 1 and v[1] + 1 or v[3] == 3 and v[1] - 1 or 1
    v[2] = v[3] == 2 and v[2] + 1 or v[3] == 4 and v[2] - 1 or 1
end
This code will now make it return 1 if both "v[3] == 1" and "v[3] == 3" return true (the "or 1"-part does that).

User avatar
zorg
Party member
Posts: 2993
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: love2d thinks the number "3" is a bool

Post by zorg » Tue Apr 12, 2016 4:33 pm

DPlayer234 wrote:This code will now make it return 1 if both "v[3] == 1" and "v[3] == 3" return true (the "or 1"-part does that).
You mean it'll return 1 if both return false; if both returned true, then it'd set the value to the first in the chain.
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
DPlayer234
Prole
Posts: 4
Joined: Tue Apr 12, 2016 3:36 pm
Contact:

Re: love2d thinks the number "3" is a bool

Post by DPlayer234 » Tue Apr 12, 2016 5:30 pm

zorg wrote:
DPlayer234 wrote:This code will now make it return 1 if both "v[3] == 1" and "v[3] == 3" return true (the "or 1"-part does that).
You mean it'll return 1 if both return false; if both returned true, then it'd set the value to the first in the chain.
Oh yeah, right. Of course. My bad.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 25 guests