Out of bounds error from checking a pixel in an image

Godot Version

Godot Version 4.4.1

Question

Hi, I have a lengthy set of if statements to check pixels for their color. I get hundreds of error messages about checking a pixel that is out of bounds.

Error: “world_generation.gd:187 @ trim(): Index p_x = -1 is out of bounds (width = 1155).”

I tried rectifying this, but after writing a few statements to handle the errors, trying to fix the errors in this function will get out of hand and take up a lot of space. Is there a way to stop this error from showing up? I suppose one thing I can do is write a function for what’s inside of the if statements and call it instead of copy and pasting lines of code when I make else-if statements to handle the out of bounds error.

Here’s an example of the code I have that’s producing the errors.


	for coordinate in traced_land:
		var x = coordinate[0]
		var y = coordinate[1]


		if	world_image.get_pixel(x-1, y+1) == Color(0.0, 0.0, 0.0, 1.0):
				var xb = x-1
				var yb = y+1
				var n = 0

				if world_image.get_pixel(xb, yb-1) == Color(0.0, 0.0, 0.0, 1.0):
					n += 1
				if world_image.get_pixel(xb+1, yb-1) == Color(0.0, 0.0, 0.0, 1.0):
					n += 1
				if world_image.get_pixel(xb+1, yb) == Color(0.0, 0.0, 0.0, 1.0):
					n += 1
				if world_image.get_pixel(xb+1, yb+1) == Color(0.0, 0.0, 0.0, 1.0):
					n += 1
				if world_image.get_pixel(xb, yb+1) == Color(0.0, 0.0, 0.0, 1.0):
					n += 1
				if world_image.get_pixel(xb-1, yb+1) == Color(0.0, 0.0, 0.0, 1.0):
					n += 1
				if world_image.get_pixel(xb-1, yb) == Color(0.0, 0.0, 0.0, 1.0):
					n += 1
				if world_image.get_pixel(xb-1, yb-1) == Color(0.0, 0.0, 0.0, 1.0):
					n += 1
				
				if n == 7:
					var r = randi_range(1, 4)
					if r == 1 or r == 2 or r == 3 :
						world_image.set_pixel(x, y, Color(0.0, 0.0, 0.0, 1.0))

This is one of four if statements. It would get out of hand to set up else-if lines.

There are several things that stand out to me here. First, you don’t have to type out the color code like that every single time. You can just do Color.BLACK instead.

Second, as far as I can tell, you never check to make sure the pixels are actually in bound.
If X is 0, you will immediately subtract 1 from it, which will end up as being -1, which is, of cours, an invalid index.

And you can most likely replace the many if statements that all do essentially the same thing with a for loop in my opinion.

But to answer your question, make sure you check for index bounds. You should not ignore or “stop the error from showing up”, it shows up for a reason, because something doesn’t work the way you’d expect it to work.

Thanks. I addressed it like this.

for coordinate in traced_land:
		var x = coordinate[0]
		var y = coordinate[1]
		
	if x == 0 or x == (image_width - 1) or y == 0 or y == (image_height - 1):
           pass

    elif world_image.get_pixel(x, y-1) == Color(1.0, 1.0, 1.0, 1.0):
			
         if 	world_image.get_pixel(x+1, y-1) == Color(0.0, 0.0, 0.0, 1.0):
			
				var xb = x+1
				var yb = y-1
				var n = 0
				

				if xb == 0 or xb == (image_width - 1) or yb == 0 or yb ==            (image_height - 1):
				pass
					
				else:

					if world_image.get_pixel(xb, yb-1) == Color(0.0, 0.0, 0.0, 1.0):

(...)