0 votes

It seems that I can't do

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

or

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.

asked Feb 20, 2018 in Projects by LockManipulator (39 points)

1 Answer

0 votes
Best answer

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
answered Feb 20, 2018 by rolfpancake (1,006 points)
edited Feb 20, 2018 by rolfpancake

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?

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

Ah ok thanks!

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

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.