Why does an Array that prints (0, 0) not also print True to array.has(Vector2.ZERO)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ChildLearning.Club

I apologize for my wording of the question, as I do not know how best to ask it. I’m having a difficult time understanding maybe how arrays work in general.

I have a timer that when timeout will append to an array the Vector2 position of each image that I have. if the images start in position (0, 0) the following code snippet prints that the array contains a Vector2(0, 0).

func _on_ImagePlacedTimer_timeout():
for area_node in part_instance_array:
	var node = area_node.get_node("Part")
	node_position.append(node.position)
print(node_position.has(Vector2.ZERO))
node_position.clear()

If I do not change the position of the image and it remains at (0, 0) the above timer will continue to print True even though I clear the array after every timeout. If I however move the image away and back to the position (0, 0) the above code will then report that it does not contain a Vector2(0, 0).

With the follow code snippet I get back “True”, which is my desired result, after moving the image away from and back to position (0, 0).

func _on_ImagePlacedTimer_timeout():
for area_node in part_instance_array:
	var node = area_node.get_node("Part")
	node_position.append(Vector2(int(node.position.x), int(node.position.y)))
print(node_position.has(Vector2.ZERO))
node_position.clear()

I’m confused as to why the first code snippet would continue to print “True” until after I moved the images positions?

If I do not change the position of the image and it remains at (0, 0) the above timer will continue to print True even though I clear the array after every timeout.    

You clear the array after every timeout but you also rebuild it on every timeout so if it finds (0,0) it will of course print true.

If I however move the image away and back to the position (0, 0) the above code will   then report that it does not contain a Vector2(0, 0).

Try dumping out the contents of the array to see if in fact you do have a (0,0) in it.

for n in node_position: 
    print(n)

LeslieS | 2023-01-29 02:53

Hi LeslieS, thank you for your response.

for n in node_position: 
    print(n)

will print anywhere between (0, 0) and (-0, -0). After I have moved it away and then back into that position. I have another function that runs that brings the node back to position (0, 0) if the image is within range.

You clear the array after every timeout but you also rebuild it on every timeout so if it finds (0,0) it will of course print true.

This I expected, what was unexpected, was that after moving the image away from its original (0, 0) position and then moving it back to (0, 0), that it does not still print True, even though your above code snippet will print (0, 0). This lead me to believe that the (0, 0) that node_position.append(node.position) was adding into the array was not the same as Vector2.ZERO. possible it is entered as a string. But if that is the case, why does it still print True on every timeout when I am clearing and rebuilding the array with node_position.append(node.position) why does it not print False after the first time the array is cleared and repopulated with node_position.append(node.position)?

Video for reference

Thank you again for your input.

ChildLearning.Club | 2023-01-29 03:46

How certain are you that those are (0,0)?
If you do this:

print(Vector2(0.00000001, 0.00000001))     

… it will print (0,0)

Try adding the floor method to the node position:

node_position.append(node.position.floor())

LeslieS | 2023-01-29 07:09

I see, yes node_position.append(node.position.floor()) does return True… my code to return the image back to (0, 0) node.position = lerp(node.position, Vector2.ZERO, 10 * delta) then does it using a float, which makes sense.

I really appreciate the clarification LeslieS. Thank you!

Edit: I also updated the question to more closely match how it should have been worded to begin with :slight_smile:

ChildLearning.Club | 2023-01-29 07:25

:bust_in_silhouette: Reply From: ChildLearning.Club

As Answered by LeslieS an array with Vector2(0.00000001, 0.00000001) will print (0, 0), but is not equal to Vector2.ZERO which will also print (0, 0). In order for them to be equal the points need to be converted to integers in something similar to Vector2(int(position.x), int(position.y)) or a function like position.floor()

Related, there’s also is_equal_approx() for doing fuzzy comparisons…

Vector2 — Godot Engine (stable) documentation in English

jgodfrey | 2023-01-29 15:35

jgodfrey, thank you, I was not familiar with that function either. That is also very useful.

ChildLearning.Club | 2023-01-30 23:24