# How can I set my variable based on index?

**URL:** <https://forums.unrealengine.com/t/how-can-i-set-my-variable-based-on-index/1327166>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, Array, Blueprint\
**Created:** [October 9, 2023, 1:50pm UTC](https://forums.unrealengine.com/t/how-can-i-set-my-variable-based-on-index/1327166 "2023-10-09T13:50:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![1nsaneEExituuuU](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@1nsaneEExituuuU](https://forums.unrealengine.com/u/1nsaneEExituuuU)\
**Post date:** [October 9, 2023, 1:50pm UTC](https://forums.unrealengine.com/t/how-can-i-set-my-variable-based-on-index/1327166/1 "2023-10-09T13:50:12Z")

</div>

I have a bunch of skills in my game.

I use big arrays with all of the variables for many of my functions.  
i.e  
“current health experience”  
“current strength experience”  
etc

it’s easy to just get value to whatever index our loop is running.  
Loop 1: will hold and do everything health related.  
Loop 2: will hold and do everything strength related

Now at the end of each loop we basically count our “current level” based on our experience. This value should be set as the current level. However this is where i run into trouble. How do i set a variable based off of the index? Can i make an array of “sets” sorta like we make an array of “gets”?

I realize this must be simple stuff, but I’m learning ^^

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [October 9, 2023, 2:00pm UTC](https://forums.unrealengine.com/t/how-can-i-set-my-variable-based-on-index/1327166/2 "2023-10-09T14:00:52Z")

</div>

> [@1nsaneEExituuuU](#):
>
> How do i set a variable based off of the index? Can i make an array of “sets” sorta like we make an array of “gets”?

I did not really understand the explanation, but this part should be just this:

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/d/c/7dc6e02538cc319caf8026dbdf093386f1ed6faa.png)

---

<div class="post-metadata">

**Author:** ![1nsaneEExituuuU](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@1nsaneEExituuuU](https://forums.unrealengine.com/u/1nsaneEExituuuU)\
**Post date:** [October 9, 2023, 2:09pm UTC](https://forums.unrealengine.com/t/how-can-i-set-my-variable-based-on-index/1327166/3 "2023-10-09T14:09:45Z")

</div>

Lets say i have this array:

get current vitality lvl [1]  
get current strength lvl [1]  
get current defence lvl [1]

Now we want to update our current defence lvl’s value to “counting current lvl [83]”

if we use the function u mention, wouldn’t that basically remove the variable reference to current defence lvl?

get current vitality lvl [1]  
get current strength lvl [1]  
get counting current lvl [83]

So that later when I need the current defence lvl from my character stats sheet, it would mistakenly return 1 to me. That’s the problem im imagining.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [October 9, 2023, 2:14pm UTC](https://forums.unrealengine.com/t/how-can-i-set-my-variable-based-on-index/1327166/4 "2023-10-09T14:14:49Z")

</div>

> [@1nsaneEExituuuU](#):
>
> wouldn’t that basically remove the variable reference to current defence lvl?

I honestly do not know what this means. The suggested script assigns a new value to an array element at specified index, which is this:

> How do i set a variable based off of the index?

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [October 9, 2023, 2:15pm UTC](https://forums.unrealengine.com/t/how-can-i-set-my-variable-based-on-index/1327166/5 "2023-10-09T14:15:35Z")

</div>

> [@1nsaneEExituuuU](#):
>
> So that later when I need the current defence lvl from my character stats sheet, it would mistakenly return 1 to me. That’s the problem im imagining.

Sounds like you want to have 2 arrays? One with default values and one with modified values?

---

<div class="post-metadata">

**Author:** ![1nsaneEExituuuU](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@1nsaneEExituuuU](https://forums.unrealengine.com/u/1nsaneEExituuuU)\
**Post date:** [October 9, 2023, 2:35pm UTC](https://forums.unrealengine.com/t/how-can-i-set-my-variable-based-on-index/1327166/6 "2023-10-09T14:35:43Z")

</div>

No

I don’t care about the default values

I care about how u access those values

Lets call a variable a key:value pair

key:value  
Current\_vitality\_level:1

Now whenever I want to check my characters vitality level i reference it by its key.

Im worried about the function u suggesting replacing the key of the variable

essentially i want this  
Current\_vitality\_level:1 → Current\_vitality\_level:83

and im worried about this:  
Current\_vitality\_level:1 → level\_counter:83

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [October 9, 2023, 3:00pm UTC](https://forums.unrealengine.com/t/how-can-i-set-my-variable-based-on-index/1327166/7 "2023-10-09T15:00:57Z")

</div>

> [@1nsaneEExituuuU](#):
>
> key:value pair

Are you sure we are talking about arrays? We started with indexes and now we have a `key:value` pair, as if you were talking about a tmap.

> **[TMap](https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/ProgrammingWithCPP/UnrealArchitecture/TMap/)**
>
> TMaps are primarily defined by two types — a key type and a value type — which are stored as associated pairs in the map.

Could you clarify which container you actually mean?

* * *

> Im worried about the function u suggesting replacing the key of the variable

If we are, indeed, talking about maps, you can only remove the key explicitly:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/c/b/6cb8873fb4dae141c8c1654286d70a0090dbb9ff.png)

Adding would override the value at key, but would do nothing to the key.

---

<div class="post-metadata">

**Author:** ![gardian206](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gardian206](https://forums.unrealengine.com/u/gardian206)\
**Post date:** [October 9, 2023, 4:07pm UTC](https://forums.unrealengine.com/t/how-can-i-set-my-variable-based-on-index/1327166/8 "2023-10-09T16:07:12Z")

</div>

if these are to be held and referenced consistently, and the order “shall” always be the same then I would suggest holding these not in an Array/Map/Set  
Arrays/Maps/Sets are best for things that it is ok for some to be there, and some to not, and maybe be ordered randomly (even though sorting can be a thing (does it make sense for an entity to have Strength but not Health, or to have Agility but not Strength ?)  
the options instead of an array

- separate member variables: this way you can specifically grab the specific one you want by name, and then work with it.
- a struct of variables: similar to the separate member variables, but these will always be together, and if you want to say put these with a pawn instead of a character you can just add the struct instead of adding each member variable (something can easily be missed)
- an ActorComponent that holds these variables: instead of a struct you give these member variables to an Actor Component the value in doing this is instead of having to cast to a specific Actor/Pawn/Character you can check to see if it has an Actor Component and talk to that. this way you lower your memory footprint, and avoid hard references in your blueprints (casting is the biggest culprit in blueprint memory overhead)

for how to get the correct one for modifying/referencing the value I would suggest an Enumeration then a `switch-case` (pull off of an Enumeration, or an `Get()` of an integer type variable and look for “switch” ) to get the specific variable you want.

---

<div class="post-metadata">

**Author:** ![1nsaneEExituuuU](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@1nsaneEExituuuU](https://forums.unrealengine.com/u/1nsaneEExituuuU)\
**Post date:** [October 9, 2023, 8:58pm UTC](https://forums.unrealengine.com/t/how-can-i-set-my-variable-based-on-index/1327166/9 "2023-10-09T20:58:27Z")

</div>

My initial thought was a bunch of branches, which would look like hell, as such I was really looking for a better control structure, but obviously the control structure I was looking for was simply an int switch case as u mentioned.

Maybe my first posts were a bit confusing. I was more trying to verbalise my issues than necessarily looking for an array solution.

Just to explain a bit better than before

The character always wants all the skills.

We are simply running some functionality at start to turn exp into a level and then storing that in a specific variable to the skill type.

Imagine  
vitality exp: 100k  
strength exp: 100

Vitality can we reach level 2? yes then 3? yes then 4? yes etc until it fails. This variable is stored in a generic counter. Lets say we end at level 60. We then want to store our current level for vitality as 60. Then reset our generic counter to 1 and continue to the next loop body.

Strength would maybe reach level 3 and then stop but would use the same counter.

Edit:  
Forgot to thank you all for ya help.  
And I cannot seem to find how to highlight the solution

---

<div class="post-metadata">

**Author:** ![xCANAdan](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@xCANAdan](https://forums.unrealengine.com/u/xCANAdan)\
**Post date:** [October 12, 2023, 7:36am UTC](https://forums.unrealengine.com/t/how-can-i-set-my-variable-based-on-index/1327166/10 "2023-10-12T07:36:56Z")

</div>

you can mark something as solution. its near the reply or heart button.

tho from what i was reading, what might be a better method than using a switch (as that will limit leveling and be a pain to setup), why not just calculate on the fly? for example, `pow(xp/100, 1.2)`. lv 1 would be 100xp, lv 2 would be 230xp, lv3 would be 370xp, ect. you can sorta do whatever, but this would be alot simpler to manage than an array of predefined values.

---

<div class="post-metadata">

**Author:** ![8c4126936c5f4146beb0af0c590a62c4](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@8c4126936c5f4146beb0af0c590a62c4](https://forums.unrealengine.com/u/8c4126936c5f4146beb0af0c590a62c4)\
**Post date:** [July 22, 2025, 1:03pm UTC](https://forums.unrealengine.com/t/how-can-i-set-my-variable-based-on-index/1327166/11 "2025-07-22T13:03:02Z")

</div>

You can try use “Set By-Ref Var” node to set the value of a variable like this:

 ![Weixin Image_2025-07-22_130147_142](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/4/1/2417ccd4d85b47251800bb7ff82634fcca173b9f.png)
