+1 vote

Basically what I want to do is:

if (array[i] index exists):
  do something
else:
  do something else

Can anyone help please? Thank you for reading.

in Engine by (90 points)

2 Answers

+2 votes
Best answer

Arrays are ordered, so an array will always contain indices from 0 to array.size() - 1

by (19,711 points)
selected by
0 votes

If you want to check if an array has a certain index or not;

var array = []
var index: int

if range(array.size()).has(index):
  print()

index can be any number of your choosing and the statement will return a bool based on whether the array has the index or not.

You can also replace 'index' with the iterator variable if the above statement is inside a for loop. It would need to be done this way:

for i in range(array.size()):
  if range(array.size()).has(i):
    print()
by (26 points)
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.
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 webmaster@godotengine.org with your username.