Page 1 of 1

love2d thinks the number "3" is a bool

Posted: Mon Apr 11, 2016 9:29 pm
by fixylol
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 229 times
Edit: nevermind, i was dumb

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

Posted: Mon Apr 11, 2016 10:06 pm
by DaedalusYoung
This:

Code: Select all

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

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

Posted: Mon Apr 11, 2016 10:07 pm
by fixylol
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 :/

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

Posted: Tue Apr 12, 2016 1:04 am
by TheOdyssey
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

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

Posted: Tue Apr 12, 2016 1:55 am
by zorg
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.

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

Posted: Tue Apr 12, 2016 7:21 am
by airstruck
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).

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

Posted: Tue Apr 12, 2016 8:31 am
by vrld

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?

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

Posted: Tue Apr 12, 2016 3:43 pm
by DPlayer234
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).

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

Posted: Tue Apr 12, 2016 4:33 pm
by zorg
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.

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

Posted: Tue Apr 12, 2016 5:30 pm
by DPlayer234
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.