Understanding Boolean Logic

Godot Version

4.3

Question

So, I’ve identified that the likely cause for my FOV not working is due to this code below, but my brain just can’t wrap itself around it, as I’m currently working on a new way to display tile data and walls. So the below code is the old way the FOV generated, and I’m trying to understand the logic for the if-else statements.

		if blocked:
			..if not current_tile.is_transparent():   
				....next_start_slope = r_slope
				....continue
			..else:
				....blocked = false
				....start_slope = next_start_slope
		elif not current_tile.is_transparent():
			..blocked = true
			..next_start_slope = r_slope

The … is an indent and … is a double indent.

So is_transparent returns a true, if its an empty tile and returns a false if it is a wall, in the old system.

So… my questions are:

  1. First block of code (if blocked). If the tile is transparent (true), which branch does it flow to? The if branch or the else branch?

  2. Second block of code (elif). If the tile is transparent (true), does it perform the following code?

I generally understand Booleans and comparisons, but this code is making my brain hurt trying to understand it, and I don’t otherwise know what to do.

If I understand what this logic is trying to “say”, I could probably address the code issue in the new code.

If the new code is desired, I can put it in a text file.

  1. Since it is transparent, but you have used NOT, the line says “if true do this”, but it is false, so the else branch is applied.

  2. No. Your term blocked was true, so only the true branch is executed. The second block is skipped.

Just think about it like this:

if TRUE:
     do this....
else:
     do this..

Everything else is just a complication, like nesting (multiple embedded if’s) or negations (use of NOT) of multiple ELSEIF’s.

Your (admittedly messy if statement) is:

if blocked:
     do whatever is here....
elif tile_is_transparent:
     do whatever is here....

Only one branch will be executed.

It might be easier to read if you do this (but this is still a mess):

if current_tile.is_transparent():  
	if blocked:
		blocked = false
		start_slope = next_start_slope
else:
	# Not transparent
	next_start_slope = r_slope
	if blocked:
		continue
	else:
		blocked = true

However that is still a messy block. Personally I never nest, but would put them into functions. Look how more readable (and in the future maintainable) this would be:

if blocked:
	handle_blocked_tile()
else:
	handle_unblocked_tile()

You also have a continue in the middle of a nested if as well, which is very messy code. You would have to clear up the rest of this loop as well.

Anyway, I hope that helped in some way.

EDIT:
It also helps if you are getting confused to label the branches with comments like this.


if blocked:
	if not current_tile.is_transparent():   
		# Blocked and NOT Transparent
		next_start_slope = r_slope
		continue
	else: 
		# Blocked and Transparent
		blocked = false
		start_slope = next_start_slope
elif not current_tile.is_transparent():
	# NOT Blocked and NOT transparent
	blocked = true
	next_start_slope = r_slope
1 Like

Thank you very much for your indepth reply. The code came from a tutorial that simply worked, as long as you didn’t touch anything. But as I was turning walls from a tile, into an object, so that I could have destructible terrain later, I had to make changes to the FOV code to make that work.

And… most of the tutorial code wasn’t commented and didn’t always use a consistent naming of variables or functions, so in the process of cleaning up the code, I am naming things consistently and commenting where necessary.

I was personally trying to do what you suggested:

if blocked:
handle_blocked_tile()
else:
handle_unblocked_tile()

… But as I didn’t really understand the weird nesting of if/elses, I wasn’t able to brute force a solution. Now that I understand what that mess of code is saying, thanks to you, I should be able to proceed and fix up my new code as well.

If not, I’ll post the new code on the forum and ask for help there.

1 Like