+1 vote

I am making a match statement that compares a vector's x and y values, however, it only works for my x-axis.

print(distance_to)
print(vector2_to_string_match(distance_to))
var y_axis = stepify(distance_to.y, 0.1)
var x_axis = stepify(distance_to.x, 0.1)
print(str("y axis = ", y_axis, ", x axis = ", x_axis, "\ny_axis == -0.7 which is ", y_axis == -0.7,
    "\nx_axis == -0.8 which is ", x_axis == -0.8))

the results give me

(-0.754222, -0.656619)
y axis = -0.7, x axis = -0.8
yaxis == -0.7 which is False
x
axis == -0.8 which is True

I recreated it in a new godot project and it's still working like this.

var vector = Vector2(-0.751234, -0.6545)
var y_axis = stepify(vector.y, 0.1)
var x_axis = stepify(vector.x, 0.1)

func _ready() -> void:
    print(x_axis == -0.8) # returns True
    print(y_axis == -0.7)# returns False
Godot version 3.2.1
in Engine by (20 points)
edited by

should the x_axis not be returning .6?

na if its 0.5 it rounds up.
so it's its 0.65 it would round the 5 up to be 0.7.
I believe that's how it works at least.

1 Answer

0 votes
Best answer

Answering your Question LIVE on YouTube: https://www.youtube.com/watch?v=fFQLwxayunI

comparing floats is always a tricky business. I would guess that you have some floating point values from this operation that are causing issues. do you need to use == exactly?

prints(x_axis == -0.8, y_axis, is_equal_approx(y_axis, -0.7))

is_equal_approx() fixes the issue

by (577 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.