Find an index in an array?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JieKou

Hi, I’m learning Godot script and I was asking myself if it was possible to return a array index from the value in? Idk if I am clear…

For example :

var array
var name

array = ["John", "Mathilde", "Marc", "Sophie"]

name = "John"

How can I find the array[index] from “John”?

Are you trying to find the position of a certain element in the array? If so, have you tried the find() function?

Ertain | 2020-07-15 16:11

:bust_in_silhouette: Reply From: dustin

you can get the index by using the find() array function.

example:

var array = ["john", "maria", "kyle", "jenny"]
var name = "kyle"

onready var index = array.find(name,0) #this will return an index no. or -1 if its not found.

check the godot docs for more details. array docs here

hope this helps!

Thank you Dustin and Ertain ! I didn’t know the function find(), I will get informations about. It seems it is what I need!

JieKou | 2020-07-15 17:08

glad we helped! gamers help gamer and thats what makes us gamers.

dustin | 2020-07-15 17:12