How can I set my variable based on index?

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 ^^

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

image

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.

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?

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

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

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.

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:

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

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.

1 Like

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

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.