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

### love2d thinks the number "3" is a bool

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
Edit: nevermind, i was dumb
Last edited by fixylol on Wed Apr 13, 2016 5:57 pm, edited 1 time in total.

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

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

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

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 :/

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

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

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


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

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 True 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.

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

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).

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

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

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

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

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).

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

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 True 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.

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

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

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.

### Who is online

Users browsing this forum: No registered users and 24 guests