How to call variable based on a "Frankenstein" of strings?

Godot Version

4.2.2

Question

So let’s say I have 6 variables:

Enemy1HP
Enemy2HP
Enemy3HP
Enemy4HP
CurrentEnemyBeingAttacked
DamageDealt

and I want to mix an “Enemy” string with “CurrentEnemyBeingAttacked” and “HP” to create a variable which I can call!
Something akin this:

var ("Enemy" + str(CurrentEnemyBeingAttacked) + "HP") -= DamageDealt

Is there any way I could do something like this? Any help is appreciated.

U want to mix the “Enemy” string with a “CurrentEnemyBeingAttacked” and “HP”
depends, enemy is only a string, if “CurrentEnemyBeingAttacked” and “HP” are strings you can just store them in a variable all together. I want to suppose that “CurrentEnemyBeingAttacked” is a Boolean and “HP” is an integer. But i dont think this is correct: var (“Enemy” + str(CurrentEnemyBeingAttacked) + “HP”) -= DamageDealt

You want to mix all that to create a variable name? (“Enemy” + str(CurrentEnemyBeingAttacked) + “HP”)

1 Like

No, not to create, to call. As in, Enemy3HP is already a thing and i just want to have something call a different enemy hp variable depending on which number CurrentEnemyBeingAttacked is.

LIke, if CurrentEnemyBeingAttacked = 3, you want to call the Enemy3HP?

Exactly.

Sounds like using an array will do you better

var enemies_hp: Array[float] = [100, 100, 100, 100]
enemies_hp[CurrentEnemyBeingAttacked] -= DamageDealth

Without an array you must use get and set

var enemy_str = "Enemy" + str(CurrentEnemyBeingAttaked) + "HP"
var current_hp = get(enemy_str)
set(enemy_str, current_hp - DamageDealt)
2 Likes

Yo broski, i have a problem too, could you help me?

The array tip was great actually, completely forgot I could just do that! Thank you.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.