Topic was automatically imported from the old Question2Answer platform.
Asked By
BigBadWouf
Hello.
I have two questions.
I have a Vector3 variable containing a position, and another Array variable containing many Vector3. For example, i call first variable “position” and second “grid”. These contains :
I’m not sure how the array has method works under the covers, but I assume it’s being tripped up by trying to do an equality check on a floating point value (in this case) - which is likely to fail randomly.
While there might be a better way, the Vector3 datatype has a is_equal_approx ( Vector3 v ) method that’s designed to return true when 2 Vector3 values are approximately equal. You could certainly write your own function that accepts a candidate Vector3 and then iterates through your Vector3 array checking each member via is_equal_approx to get what your after…
Here’s a rough example of what I mentioned above…
func _ready():
var a = []
a.append(Vector3(1.0, 1.0, 1.0))
a.append(Vector3(1.5, 1.5, 1.5))
a.append(Vector3(2.00001, 2.00001, 2.00001))
print(vec3_array_has(a, Vector3(2,2,2)))
func vec3_array_has(vec3_arr: Array, vec3: Vector3) -> bool:
for v in vec3_arr:
v = v as Vector3
if v.is_equal_approx(vec3):
return true
return false
jgodfrey | 2022-02-06 23:06
It’s exactly what i’m doing now.
That work well.
But i’m curious to know why sometime it product this stranges float.
Anyway, thank you very much for answer, explanations and example.