# if statement not being reached

### Question

if statement not being reached even tough conditions are being met

extends Sprite2D
var x_position := randi_range(0,1280)
var y_position := randi_range(0,720)
var edgeofscreen = Vector2(0,800)
@export var speed := 20

position = Vector2(x_position,y_position)
edgeofscreen = Vector2(x_position,800)

func _process(delta):
position += Vector2(0,1) * speed * delta
print(position)
print(edgeofscreen)
if position == edgeofscreen:
position += Vector2(x_position,-100)
print(â€śaâ€ť)

1 Like

Use code format for posting code.

Itâ€™s almost impossible to be equivalent, just think about it. You multiplied it by `delta` which is a float, and how could they be precisely equivalent? Instead, you should use the `is_equal_approx()` function, for approximate comparison.

4 Likes

u can try this for float points

``````extends Sprite2D

var x_position := randi_range(0, 1280)
var y_position := randi_range(0, 720)
var edgeofscreen = Vector2(0, 800)
@export var speed := 20

position = Vector2(x_position, y_position)
edgeofscreen = Vector2(x_position, 800)

func _process(delta):
position += Vector2(0, 1) * speed * delta
print(position)
print(edgeofscreen)

# Define a small threshold to account for floating-point inaccuracies
var threshold := 1.0
if position.distance_to(edgeofscreen) < threshold:
position += Vector2(x_position, -100)
print("a")

``````

changes:

``````var threshold := 1.0

``````

Introduced a small threshold (threshold := 1.0) to handle floating-point inaccuracies which can cause the condition to never be met due to minor differences in position values.

``````if position.distance_to(edgeofscreen) < threshold:

``````

Changed the comparison from exact equality (if position == edgeofscreen) to a proximity check using distance_to. This ensures that the condition is met when position is within a small distance from edgeofscreen, accounting for any minor discrepancies.

maybe try this and see if can help.
Nasshor

1 Like

In addition to what the others have said, the problem in getting the values to be equal is that youâ€™re adding a fixed amount to position each frame and unless the exact coordinates of the edge of screen are extremely close to being equal to

``````(Initial position)  + (some integer) *speed*delta
``````

Youâ€™d never get the two to even be approximately equal (i.e. within reasonable floating point error). So youâ€™ll either need to slow the movement down as you near the edge of the screen or use a buffer of probably at least half of speed*delta.

1 Like

A better choice would be using `distance_squared_to()`, for performance considerations.

``````var threshold := 1.0
var threshold_squared := threshold * threshold
if point_a.distance_squared_to(point_b) < threshold_squared
``````
1 Like

yeah this can works fine, using `distance_squared_to()`improves performance, especially in scenarios with many distance checks.

which checks the squared distance between `position` and `edgeofscreen`.

i can totaly agree with that

1 Like

thank you for helping me guys