Better way to write these kind of bool checks?

Godot Version

4.5

Question

front/back_areas are Raycast bools.Thanks.

if front_area == false and back_area == false:
	edge_area = false
elif front_area == false and back_area == true:
	edge_area = true
elif front_area == true and back_area == false:
	edge_area = true
elif front_area == true and back_area == true:
	edge_area = false
edge_area = (front_area != back_area)
3 Likes

What you’re looking for is called an “exclusive or” or “XOR” and you can do it using the != operator as per the docs.

3 Likes

A bitwise xor operator could be used although != is a bit less cryptic.

edge_area = bool(int(front_area) ^ int(back_area))
3 Likes

I actually said holy sht out loud of how simple it is (to write):+1: !! I mean I know != is used to toggle but didn’t know it could be used this way.(still can’t wrap my head around on how this works though..)

** wait I think I got it.

edge_area bool uses the result from both front/back_areas bools.

if front/back_areas are the same then edge_area is false, and toggle true if front/back_areas aren’t the same.

2 Likes

front_area != back_area is a boolean expression that evaluates to a boolean true/false value. You can assign that value to a boolean variable.

1 Like

Yea , I just explained this to myself like I’m 5 (in my edits). I’m also leaving my original codes as an example of what it does in case I forgot.LOL

edge_area = (front_area != back_area)
#if front_area == false and back_area == false:
	#edge_area = false
#elif front_area == false and back_area == true:
	#edge_area = true
#elif front_area == true and back_area == false:
	#edge_area = true
#elif front_area == true and back_area == true:
	#edge_area = false
2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.