# Is there a way to iterate over multiple vector variables xyz values?

Attention Topic was automatically imported from the old Question2Answer platform.

Lets say I want to modify a bunch of Vector3 variables and I want to change each of their x, y, and z values independently. Right now I have to either write the same code out three times, once for each axis, or create a function and pass into it a whole ton of variables. This isn’t a big deal but it has been bothering me… It seems like there must be a way to do it!

Examples; what I am doing now:

``````if(foo.x > 5):
bar.x = foo.x
eggs.x += bar.x
foo.x = ham.x

if(foo.y > 5):
bar.y = foo.y
eggs.y += bar.y
foo.y = ham.y

if(foo.z > 5):
bar.z = foo.z
eggs.z += bar.z
foo.z = ham.z
``````

I would love to do something like this but I can’t figure out how to make it happen:

``````for axis in xyz:
if(foo.axis > 5):
bar.axis = foo.axis
eggs.axis += bar.axis
foo.axis = ham.axis
``````

Thank you!!!

Hi
this wont be faster but should be working

``````var xyz = ["x","y","z"]

for axis in xyz:
if(foo.get(axis) > 5):
bar.set(axis, foo.get(axis))
eggs.set(axis, eggs.get(axis) + bar.get(axis))
foo.set(axis, ham.get(axis))
``````

This is actually wrong!

Have a look at timothybrentwood answer below!

Yeah, it’s better than what I was doing though! Thank you!

path9263 | 2021-08-16 05:23

This shouldn’t work on Vectors since they don’t have a `get()` method.

timothybrentwood | 2021-08-19 03:46

You can access x, y, and z using the `[]` operator:

``````for component in range(3):
if foo[component] > 5:
bar[component] = foo[component]
eggs[component] += bar[component]
foo[component] = ham[component]
``````

You are right … as statet here in the docs

GDScript reference — Godot Engine (stable) documentation in English

klaas | 2021-08-19 12:13