Checking if key exists in map give faulty results

I’ve got some Map structure that looks like this:
var edges:Map<Vector2, List<Vector2> >;
When I add some values, like:
edges[new Vector2( 100, 100)] = new Vector2(300, 500);
and then check if edges.exists(new Vector2( 100, 100 ) ) I got false as a return.

How are the Keys compared then?
Is there any way to make it work?
Why it doesn’t?

Edit1:
Omg. Why does new Vector2( 100, 100).equals( new Vector2( 100, 100) ) returns true
while new Vector2( 100, 100) == new Vector2( 100, 100) returns false ??

It’s because Vector2 is a class and == on objects compares the “pointer” not the actual content.

1 Like

This will work:

var vec = new Vector2 (100, 100);
edges[vec] = new Vector2 (300, 500);
trace (edges.exists (vec));

I’m not sure how you would do it by value, perhaps something like…

edges[100][100] = new Vector2 (300, 500);
var edge = edges[100];
trace (edge != null && edge.exists (100));

with a Map<Int, Map<Int, Vector2>>

As they said, comparing two instances of a class are going to be different, but perhaps a custom Map implementation (another approach?) could use the equals method when comparing

This could also work using an array I think