Code doesn't work without print statements

Godot Version

4.2.2.stable

Question

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:

	print(get_intersection_pair(a_intersections_array, district) != null)
	print(get_intersection_pair(b_intersections_array, district) != null)
	print(get_intersection_pair(c_intersections_array, district) != null)
	print(get_intersection_pair(d_intersections_array, district) != null)
	all_intersections_array = a_intersections_array + b_intersections_array + c_intersections_array + d_intersections_array
	if get_intersection_pair(a_intersections_array, district) != null:
		intersection_pairs.append_array(get_intersection_pair(a_intersections_array, district))
	if get_intersection_pair(b_intersections_array, district) != null:
		intersection_pairs.append_array(get_intersection_pair(b_intersections_array, district))
	if get_intersection_pair(c_intersections_array, district) != null:
		intersection_pairs.append_array(get_intersection_pair(c_intersections_array, district))
	if get_intersection_pair(d_intersections_array, district) != null:
		intersection_pairs.append_array(get_intersection_pair(d_intersections_array, district))

Regards Gabriel :)

what does get_intersection_pair do? Can you show that function definition?

1 Like

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.

1 Like
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
1 Like

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 :slight_smile:

I think I now understand why it worked with the print statements. Thank you very much for your help!

1 Like

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