itemInfo.Item.Category.TryGetCategoryAttributeValue only returns null for me

nathanj

Active member
Hello,

II'm trying to get the Icon from an item Category but it only returns null for me. I am trying
Code:
if(itemInfo.Item.Category.TryGetCategoryAttributeValue<Sprite>("Icon", out var icon)) {}

It's weird because I was able to use
Code:
if (itemInfo.Item.TryGetAttributeValue<Sprite>("Icon", out var icon)){}
successfully. Or, am I missing something?

Thank you,
Nathan
 
The Item category has seperate functions for getting attributes depending on what attribute collection it is on. My guess is that your Icon is part of the ItemDefinition attribute collection?

so to get its value in the category it would be:
Code:
if(itemInfo.Item.Category.TryGetDefinitionAttributeValue<Sprite>("Icon", out var icon)) {}

//If it was part of the item attribute collection it would be:
if(itemInfo.Item.Category.TryGetItemAttributeValue<Sprite>("Icon", out var icon)) {}

I realise that's a bit confusing so I will add a new function GetAttribute<T> and TryGetAttributeValue<T> that will search in all three attribute collections

EDIT:
I have now added these two functions to the ItemCategory class:
Code:
/// <summary>
/// Returns an attribute that is part of the category, item or Item Definition attributes.
/// </summary>
/// <param name="attributeName">The attribute name.</param>
/// <returns>The attribute.</returns>
public T GetAttribute<T>(string attributeName) where T : AttributeBase
{
    var attribute = GetCategoryAttribute<T>(attributeName);
    if (attribute == null) { attribute = GetDefinitionAttribute<T>(attributeName); }
    if (attribute == null) { attribute = GetItemAttribute<T>(attributeName); }
    return attribute;
}
/// <summary>
/// Tries to get an attribute that is part of the category, item or Item Definition attributes.
/// </summary>
/// <param name="attributeName">The attribute name.</param>
/// <param name="attributeValue">Outputs the attribute value.</param>
/// <returns>The true if the attribute was found.</returns>
public bool TryGetAttributeValue<T>(string attributeName, out T attributeValue)
{
    if (TryGetCategoryAttributeValue<T>(attributeName, out attributeValue)) {
        return true;
    }
    if (TryGetDefinitionAttributeValue<T>(attributeName, out attributeValue)) {
        return true;
    }
    if (TryGetItemAttributeValue<T>(attributeName, out attributeValue)) {
        return true;
    }
    return false;
}
 
Last edited:
Hi

Im trying to get the icon of the Category, not the definition. Im making a pickup up that only shows the category type at a distance.
But, the above mentioned category trygetdefinition doesn’t work, I can get the definition icon fine.
 
Can you show me a screen shot of your ItemCategory and the attribute you want to show inside the Inventory system manager editor?

That should give me a better idea of your setup
 
@Sangemdoko

My apologies, the first code you provided works, i was reading on my phone while walking down the street when I responded. I should be more patient and trust your responses.

This is awesome! Thank you,
Nathan
 
Top