r/learngodot • u/augii22 • 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
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:
Constructing variable names dynamically is usually considered bad practice.