Checking array for duplicates with GDscript

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

It seems that I can’t do

var array1 = []
if (xcoord, ycoord) not in array1:
    #do something


for i in range(array1.size()):
    if array1[i] == (xcoord, ycoord):
        #do something

where xcoord and ycoord would be just random numbers. It keeps saying “Expected ‘)’ in expression” on the if statement line after xcoord. How can I check if this set of coordinates is in the array? The array will hold around 50 items.

:bust_in_silhouette: Reply From: rolfpancake

Your not is at the wrong place: It must be written right after the if. And it should be [xcoord, ycoord] instead of (xcoord, ycoord) (with thanks @hilfazer):

var array1 = []
if not [xcoord, ycoord] in array1:
    #do something

Just a little addition: If the xcoord and ycoord values are components of a vector you can write it like that:

var vector_array = [vector1, vector2, vector3, ...]
var vector = Vector2(xcoord, ycoord)

if not vector in vector_array:
    #do something

It still doesn’t work,

if not (xcoord, ycoord) in array1:

is still saying “Expected ‘)’ in expression”. And I don’t understand what’s happening with the Vector2 code you put. What is vector1, vector2, etc?

LockManipulator | 2018-02-20 17:15

You need [], not () around your coords if your array contains 2-element arrays.
If it contains Vector2s, use this: Vector2(xcoord, ycoord)

hilfazer | 2018-02-20 17:34

Ah ok thanks!

LockManipulator | 2018-02-20 18:03

Thanks for your fast response to my mistake. I edited my answer accordingly.

rolfpancake | 2018-02-20 19:51