How to correctly use the booleans operators

Monkey Forums/Monkey Programming/How to correctly use the booleans operators

mteo77(Posted 2013) [#1]
Hello everybody.
It might seem a stupid question but since i am having a hard time playing with boolean variables i am probably missing something..
I have some code that based on a keypress start a chain of events.
Now the key press is not the only check, but at the same time i am comparing some variables that are of type bool.
Example:
If iKeyDown(KEY_LEFT) And Not checkObject(-1, 0) And solid = True

It doesn't matter how i set up the variable solid, the outcome will always be the same.
But if i change solid to an int and change it's value accordingly all works fine.
Basically what i am trying to achieve is to have some variables of type bool that are used as "flags" to perform various operations, in this case is should set an object to solid so it can't be walked over.
Am i doing something conceptually wrong or it's probably my code somewhere else?
Can't really post the code cause between trial and errors it looks like something coming out of a mince machine...


Paul - Taiphoz(Posted 2013) [#2]
Here you go.

Just make your checks clearer with handy parenthesis's, just shoved this into the bottom of my games class and when I press left, it works, unless I set solid to fale and then pressing left does not work.


		If KeyDown(KEY_LEFT) And (NOT checkObject(-1, 0)) And (solid = True)
			Print "WORKED"
		Else
			Print "DIDNT WORK"
		endif
	End
	
End

Function checkObject:bool(x:int,y:int)
	return False
end Function


think it was getting hung up on your "And Not"


computercoder(Posted 2013) [#3]
Thats what I experience too. Any time I use "Not", I follow it with the boolean evaluation inside of parenthesis; like Taiphoz explained in his example code. In BlitzMAX, you didn't need to worry about it and I kept getting tripped up on it until I realized what it was doing.


Nobuyuki(Posted 2013) [#4]
I'm actually surprised that Not doesn't have priority over the other boolean operators. This tripped me up as well.. is it trying to evaluate the entire right side of the expression?


computercoder(Posted 2013) [#5]
It seems like it is. I kept getting an error that indicated I set it up wrong, so I tried the Order of Operations based way of doing things and included parenthesis. I knew it was legal what I was trying to do, but it said otherwise. I just made it do the order of operations in the manner I expected and that solved it. So I made a note to make sure to use them with Not. The other way around using Not is to just use the evaluaters <>, meaning Not.

So instead of this:
     if Not (thisValue = "value") then...


Use this:
     if thisValue <> "value" then...


They both accomplish the same thing. The strings were always my problem with using "Not"


mteo77(Posted 2013) [#6]
Thank you very much for the explanation, i lost 4 hours yesterday evening trying to figure out why it wasn't working and at the end i gave up.


computercoder(Posted 2013) [#7]
You're welcome! Glad to be of help :)


Paul - Taiphoz(Posted 2013) [#8]
Yeah I had the same issues when coming from max, love max, but monkey is much better, I had to unlearn a few things, this being one of them.

Best bet is to simply () off any if's longer than a single condition, a lot of the time it wont technically be needed but by forcing yourself to do it you will make it a learned habit which will save you hours down the road.


computercoder(Posted 2013) [#9]
Agreed Taiphoz.

In suppport of what you are saying with always using the (), I found that it will also error even on a single condition without (). If you try to peform the following evaluation, it will result in an error "Cannot convert from Bool to String." (which is when I figured out what was going on):
Import mojo

Class MyApp Extends App

  Method OnCreate()
    SetUpdateRate 15
  End
	
  Method OnRender()

    Cls
		
    Local val:String = "Test"
		
    If Not val="test" Then              ' <--- This will cause an error
      DrawText("test was not found",0,0) 
    Else
      DrawText("test was found",0,0)
    End
          
  End

End

Function Main()

  New MyApp
	
End


But if you try this code it will work because it evaluates val="test" then negates it:
Import mojo

Class MyApp Extends App

  Method OnCreate()
    SetUpdateRate 15
  End
	
  Method OnRender()

    Cls
		
    Local val:String = "Test"
		
    If Not (val="test") Then              ' <--- This will execute as expected
      DrawText("test was not found",0,0) 
    Else
      DrawText("test was found",0,0)
    End
          
  End

End

Function Main()

  New MyApp
	
End


*smacks forehead*