Page 1 of 1
Error: attempt to compare number with boolean
Posted: Sun May 11, 2014 8:31 am
by SadBlobfish
So, I have a simple snippet of code among much more code...
Code: Select all
if (newx > self.x) or (newx < self.x) then
player.xvel = 0
end
and when I run it, I get this error...
Code: Select all
Error
player.lua:18 attempt to compare number to boolean
Alright, seems rather straightforward. But then I try...
or...
or...
to force the program to terminate and display the variables' values. And, unexpectedly, it ALWAYS returns a numeral value for each variable. So the problem is apparently isolated to the region of code I've shared with you, which is why I didn't bother to post the complete script for my game. Anyway, after staring at my laptop screen for about half an hour, I decided to try this...
Code: Select all
error((newx > self.x) or (newx < self.x))
and what do I get?...
So there is nothing wrong with the condition statement itself. I've looked around the region where the problem is occurring, and I see nothing wrong with it. There's apparently no problem with stray parenthesis, operators, end's, etc. because I can easily insert an error function directly before the if statement. All I can figure is that it may be some problem with metatables. Just shoot me with ideas, I'll try anything if you think it's worth a try.
Notes/Synopsis: This snippet of code is located in a function. newx is a variable local to the function. self is the address of a table. All variables seem to work fine until I attempt to use them in an if statement.
Re: Error: attempt to compare number with boolean
Posted: Sun May 11, 2014 1:43 pm
by davisdude
The problem has to be somewhere else. We need the rest of your code to help you. Best from I can tell, you do one of two things:
- You name the variable as false (i.e. player.xvel = false) and then update, before changing the value.
- You have some condition that is called before updating that makes it a boolean (i.e. if wallCollide( player.x, player.y, player.xvel, player.yvel, map) then player.xvel = false end)
Re: Error: attempt to compare number with boolean
Posted: Sun May 11, 2014 4:52 pm
by SadBlobfish
davisdude wrote:The problem has to be somewhere else. We need the rest of your code to help you. Best from I can tell, you do one of two things:
- You name the variable as false (i.e. player.xvel = false) and then update, before changing the value.
- You have some condition that is called before updating that makes it a boolean (i.e. if wallCollide( player.x, player.y, player.xvel, player.yvel, map) then player.xvel = false end)
That's not what's happening, though. When I used the error() function to check the values of variables, I did it DIRECTLY before the if statement that I posted. So somehow, the value of one of the variables is changing seemingly all by itself.
Also, I'll get around to posting all my source code later today. The files are on a different computer than the one I'm posting this from.
Re: Error: attempt to compare number with boolean
Posted: Sun May 11, 2014 6:34 pm
by SneakySnake
Don't use error for debugging.
It terminates the program.
The problem is most likely not in the first iteration, but at a later time.
Use assert or print instead.
Actually, error is kind of useful to see if a branch of execution is ever reached.
But that's pretty much all I can think of using it for debugging.
Re: Error: attempt to compare number with boolean
Posted: Sun May 11, 2014 11:50 pm
by SadBlobfish
SneakySnake wrote:Don't use error for debugging.
It terminates the program.
The problem is most likely not in the first iteration, but at a later time.
Use assert or print instead.
Actually, error is kind of useful to see if a branch of execution is ever reached.
But that's pretty much all I can think of using it for debugging.
I was using error() because I was having problems with love.conf(). I couldn't get the console to show up, the title of the game didn't change, etc. However, I decided to mess around with love.conf() it and I got it to work. Now, understand that the problem I was having would always occur the instant the player hit the ground, or so it seemed. I thus assumed that the function that caused the error was only getting called once. But, as I discovered when I got the console to work, the function was actually being called twice. The function executed correctly the first time, but not the second. What happened is this: in the function where the problem was, I used a function I made that will return false when the parameters it is given don't match a certain criteria; after the function executes fine the first time, it changes the value of certain variables in such a way that they do not match this criteria the next time the function is called. That's an extremely simplified explanation of what the problem was.
Anyway, I know what the problem is now. I just need to fix it. Thanks for the help, guys! I really appreciate it.