How to set unique theme type variations per instantiated scene?

Godot Version

4.5.0

Question

I have created two theme type variations for a Label: “SmallLabel”, and “MediumLabel”.
I have a scene called “CostLabel” which has a “Label” child node. In a script attached to “CostLabel” I have a function called “set_label_size”:

Scene Structure:
| - CostLabel: HBoxContainer

  • | - - Label: Label

func set_label_size(label_size: String) → void:
if not SIZES.has(label_size):
print(“CostLabel size not valid”)
else:

# type variant here is either "SmallLabel" or "MediumLabel"
$Label.set_theme_type_variation(SIZES.get(label_size).get(“type_variant”))

The idea is, when I instantiate a CostLabel, I want to be able to set a particular font size via type variation.

The problem is that the type variation gets applied to ALL instantiated CostLabel scenes. Meaning, if the last CostLabel instantiated has set_label_size called with argument “medium”, all CostLabel font sizes will be the MediumLabel size, the same is true for the “small” argument.

I’m new to Godot, so please forgive me if this is a misunderstanding on instantiated scenes. If there is a better way to achieve my goal, please let me know!

Thank you

The code looks fine, so this shouldn’t happen unless something is not correctly set up somewhere in the chain, potentially when you get the String from the dictionary.

Try to see if it works as expected with a simpler function:

func set_label_size(label_size: String) → void:
        $Label.theme_type_variation(label_size)

You’ll pass the name of the theme directly, and check that it works. For example set_label_size("SmallLabel") and set_label_size("MediumLabel") respectively.

Thanks for taking the time to reply. I realized the issue was that I was calling the method before the the CostLabel was attached to the scene tree (rookie mistake). I ended up re-architecting the CostLabel to have some properties set at instantiation, and then calling a private _set_label_size function in _ready which just used those set properties..