0 votes

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.

in Engine by (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
by (1,032 points)
edited by

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.