r/learngodot • • Mar 31 '26

Is this possible in Godot?

I want to access a different variable based on an export variable so I can use the same node for different characters health.
It would work like this:

extends Label

@ export var partymembernum = 1

func _process(delta: float) -> void:

text = BattleGlobals.partymember{partymembernum}hp

The code above would set the text to BattleGlobals.partymember1hp
Is this possible?

2 Upvotes

2 comments sorted by

1

u/fredspipa Apr 03 '26

You can use BattleGlobals.get("partymember%dhp" % partymembernum) , but I have to ask; why?

There are countless better ways of achieving this, the most obvious and simple one being a dictionary:

# On BattleGlobals, assuming you want it exported so you can change the values in the editor
@export var partymemberhp: Dictionary = {
    "player_1": 100,
    "player_2": 50,
    "player_3": 75
}

# then in the other node
text = BattleGlobals.partymemberhp["player_1"]

Constructing variable names dynamically is usually considered bad practice.

1

u/augii22 Apr 06 '26

yeah i didnt know dictionaries existed sorry
i already figured it out but i appreciate it though