How to pick from an Array?

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

I want to have a variable assigned based on the value of another variable. Because the second variable can have lot of different values, instead of having a lot of if statements, if I can, I’d rather use something that checks equivalence within anywhere in an array and picks the element within that array that matches to assign to the first variable.

So “if x equals anything in array [a, b, c, d]
y = the element x is equal to”

:bust_in_silhouette: Reply From: 1234ab

while not the most performant, you can simply

for i in array:
    if x == i:
        y = x

or better (this should also work afaik):

if x in array:
    y = x

I’m getting and Invalid get index error.

var A = "A" 
var B = "B"
var pdir = [A, B]
var pt

func _ready():
for i in pdir:
	if piece_type == pdir[i]:
		pt = pdir[i]

John97 | 2021-04-14 16:27

oh, you can just if piece_type == i:, if you do for i in pdir then i is already the string, and not the index of the string
and also pt = i
to iterate over the indices/indexes you can write smth like for i in range(len(pdir)) but only do that imo if you need the index

1234ab | 2021-04-14 16:35

I don’t want pt to equal i, I want it to equal the index, pt = A, not pt = “A”. Variables can be made equal to variables, right?

John97 | 2021-04-14 16:42

In this case, i is not the index but the item in the array.

exuin | 2021-04-14 16:48

Variables can be made equal to variable, right?

well not really, you can either make pt equal the value of A, which is "A" (if you write pt = A) or you can make it equal to the index of "A" (in the pdir Array at least), which is 0, because pdir in reality is ["A", "B"], the A and B variables’ values are used when creating the array
if pt is 0, then you can access "A" with pdir[pt]
but if you modify A to be "C", then pdir will not be updated

you can experiment with what is what by printing it out with print(variable) but there you won’t see the "s if I recall correctly

1234ab | 2021-04-14 16:50

So you’re saying I can’t make an array of variables?

John97 | 2021-04-14 16:52

yeah, you can’t (at least until someone comes here and proves me wrong :smiley: )
but you can use pdir[0] exactly as if it was a variable

but if you modify A to be “C”, then pdir will not be updated

I’m 99% sure at least, but you can try it out

1234ab | 2021-04-14 16:56

It depends, I think. Some values are passed by reference and some by value.

The docs say:

In GDScript, only base types (int, float, string and the vector types) are passed by value to functions (value is copied). Everything else (instances, arrays, dictionaries, etc) is passed as reference. Classes that inherit Reference (the default if nothing is specified) will be freed when not used, but manual memory management is allowed too if inheriting manually from Object.

exuin | 2021-04-14 16:58

trueee (but also poolarrays are by value until Godot 4)

1234ab | 2021-04-14 17:00

Looks like I’ll have to make a million if statements to accomplish what I’m trying to then.

John97 | 2021-04-14 17:03

Let’s go after the doc writers /s

exuin | 2021-04-14 17:04

Perhaps instead of having an array of variables you can just modify the array directly? If you need names you can use a dictionary as well.

exuin | 2021-04-14 17:05

Maybe a dictionary will work. I don’t know how those work yet.

John97 | 2021-04-14 17:07

I’ve got it

func _ready():
	for i in pdic:
		if piece_type == pdic[i]:
			var keyl = pdic.keys()
			for n in pdic.keys():
				if n == i:
					pt = n

John97 | 2021-04-14 18:01

Since “i” is already the key, you only have to do this:

for key in dictionary:
    if a == dictionary[key]:
        b = key

exuin | 2021-04-14 18:51