Hi everyone!
I have a really interesting question for you. The code below doesn’t work without the four print statements… Does anyone have a clue how the code only works with these print statements?
Without the print statements the code inside the if gets exectued altough the if condition is false and shouldn’t be executed. The print statements also print the correct result of the condition.
Code:
Anytime you call a function, you are making a change. Just make a line of code calling the functions without the print statement. I see you did that, sorry. I did have a problem sometime where something like that happens and I had to use some kind of force statement so it did the calculation before the next frame. Don’t remember exactly how I did it.
func get_intersection_pair(intersections: Array[Vector2], district: District):
if len(intersections) == 2:
for intersection in intersections:
if intersection.x >= district.A.x and intersection.x <= district.B.x:
if intersection.y >= district.A.y and intersection.y <= district.C.y:
intersection_pair.append(intersection)
if len(intersection_pair) == 2:
return intersection_pair
else:
return null
The function gets all intersetions of a rectangle (=district) and a circle and checks if the circle intersects a straight of the rectangle exactly 2 times. If so it returns this intersection “pair” otherwise it returns null
so it does alter a intersection_pair global variable, calling it more than once will yeild different results. Maybe it only found one intersection, but since it’s called twice it’s duplicated into the intersection_pair variable. I’m betting you wish this to be a local variable instead.
func get_intersection_pair(intersections: Array[Vector2], district: District):
var pair: Array[Vector2] = []
if len(intersections) == 2:
for intersection in intersections:
if intersection.x >= district.A.x and intersection.x <= district.B.x:
if intersection.y >= district.A.y and intersection.y <= district.C.y:
pair.append(intersection)
if len(pair) == 2:
return pair
else:
return null
Thank you very much! Using a private variable fixed the issue. Its just really weird that it worked with the print statements before… I will keep it in mind for the future if I again encounter a problem like this