Godot - Search array for value
GDScript code example to search array for value.
Can search array for value using find()
in GDScript.find()
returns index where first occurrence of specified value is found.
Returns "-1" if value is not found.
var array = ["apple", "lemon", "banana"]
var find = array.find("lemon")
print(find)
# 1
Can search in reverse order using rfind()
.rfind()
starts search from end of array and returns index of first occurrence found.
Index returned counts from beginning of array.
For multiple identical values, index of value closest to end is returned.
Returns "-1" if value is not found.find_last()
is replaced by rfind()
.
var array = ["apple", "lemon", "banana", "apple"]
var rfind = array.rfind("lemon")
print(rfind)
# 3