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.