Hiding null values from a tooltip

Hi,
I have a tooltip for my rpg inventory system. An example of how it works:

if (item.itemType == Item.ItemType.HeavyHull)
        {

            if (item.itemRarity == 1)
            {
                tooltip.transform.GetChild(0).GetComponent<Text>().text = "<color=#00ff00ff><b>" + item.itemName + "</b></color>\n\n" + "\nHealth: " + item.itemHealth + "\n" + "\nMana: "
                     + item.itemMana + "\n" + "\nArmour: " + item.itemArmour + "\n" + "\n" + item.itemSlotDesc;
            }
        }

I want to have items with varying stats- ie some wont have health, some wont have mana. Is there a way to hide any null values so they keep the tooltip clean?

Thanks.

Just break it up with more ifs?

There are more sophisticated ways, but I’m not sure you need it for this problem.

Thank you Kiwasi. That is definitely an option, but this was a simple example. I have 6 stats so doing if statements for every combination would be very time consuming.May have to just bite the bullet and get typing!

You only need one if statement per stat.

string textToDisplay;

if (item.hasStamina) {
    textToDisplay += "Stamina effect: " + item.stamina + "\n"
}
if (item.hasStrength) {
    textToDisplay += "Strength effect: " + item.strength + "\n"
}

If you want to get more sophisticated you can build an attributes class which wraps the attribute and its description, then you can build the string by adding all of the descriptions together.

1 Like