# EAxis enum and FVector indexing

**URL:** <https://forums.unrealengine.com/t/eaxis-enum-and-fvector-indexing/306691>\
**Category:** Debugging, Optimization, & Profiling\
**Tags:** Optimization, fvector, ENUM, unreal-engine, question, CPP\
**Created:** [February 11, 2015, 7:38pm UTC](https://forums.unrealengine.com/t/eaxis-enum-and-fvector-indexing/306691 "2015-02-11T19:38:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [February 11, 2015, 7:38pm UTC](https://forums.unrealengine.com/t/eaxis-enum-and-fvector-indexing/306691/1 "2015-02-11T19:38:51Z")

</div>

I am obviously trying to use the core engine types and structures whenever possible rather than duplicating things unnecessarily, however I was very surprised when I discovered that a bug I had was caused because I’d been wrong in assuming that the EAxis enumeration corresponded to the 0-based index of the components of a vector.

```
namespace EAxis
{
    enum Type
    {
        None,
        X,
        Y,
        Z,
    }
}

```

So X is 1, meaning I can’t do the following:

```
EAxis::Type axis = GetAxisFromSomewhere();
FVector vector = SomeVector;
float component = vector[axis];

```

There are heaps of times I want to manipulate components based on some runtime dependent axis. If I use EAxis, I need to either remember to add “-1” all over the place, or resort to switching instead leading to code like [this](https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Math/FMatrix/Mirror/index.html).

Then I came across the [FVector::operator[code](https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Math/FVector/op_subscript/1/index.html) and now I’m really confused. Am I completely off here and writing out hard coded accesses with if-elses is actually faster than the non-branching integer indexing alternative?

Very interested in any reasons or opinions on this.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_9a926f8e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_9a926f8e](https://forums.unrealengine.com/u/anonymous_user_9a926f8e)\
**Post date:** [February 16, 2015, 10:06pm UTC](https://forums.unrealengine.com/t/eaxis-enum-and-fvector-indexing/306691/2 "2015-02-16T22:06:30Z")

</div>

If you are in performance critical situation then you may want to use VectorRegister to get a proper SSE/NEON vector register and keep all my math work vectorized avoiding going back to scalar floats. If you need to grab just one component use VectorSelect() with the proper mask for the component you want and that will return a vector.

---

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [February 19, 2015, 10:52am UTC](https://forums.unrealengine.com/t/eaxis-enum-and-fvector-indexing/306691/3 "2015-02-19T10:52:53Z")

</div>

Thanks for your answer @PZurita. I will certainly look into VectorRegister in the future. I’m still a little confused by the 1-based enum, even ignoring performance, since it is just rather inconvenient in the given use case. Anyway I will close the question as it is by no means a major issue.
