love2d thinks the number "3" is a bool
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
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) Edit: nevermind, i was dumb
here's a love file in case you don't understand: (press P to advance to the next level) 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: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: love2d thinks the number "3" is a bool
This: seems to return false.
Code: Select all
v[3] == 2 and v[2] + 1 or v[3] == 4 and v[2] - 1
Re: love2d thinks the number "3" is a bool
it'shouldnt set the value to false though :/DaedalusYoung wrote:This:seems to return false.Code: Select all
v[3] == 2 and v[2] + 1 or v[3] == 4 and v[2] - 1
- 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
Is the exact same thing as
Code: Select all
v[2] = v[3] == 2
Code: Select all
if v[3] == 2 then
v[2] = true
end
- zorg
- Party member
- Posts: 3470
- 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;
...well, technically, you could only enclose the whole clause after the or, but this is a bit more straightforward.
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))
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.
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).
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
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
- 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)
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:
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).
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
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
- zorg
- Party member
- Posts: 3470
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: love2d thinks the number "3" is a bool
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.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).
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
Oh yeah, right. Of course. My bad.zorg wrote: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.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).
Who is online
Users browsing this forum: No registered users and 2 guests