+1 vote

Basically what I want to do is:

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

Can anyone help please? Thank you for reading.

in Engine by (90 points)

2 Answers

+1 vote
Best answer

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

by (20,479 points)
selected by
+1 vote

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):

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):
by (28 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.