Getting Enum value from item info

Bernkastel

Member
In the tutorial: Customizing UI: Item Views & Attribute Views we set the Rarity attribute as an enum. I am now trying to grab the value of that enum using trygetattributevalue, which I've been using for every other value so far, but when used in an if statement declaration it returns false. When looking at the valuetocoloritemview class, the enum is grabbed using TryGetAttribute instead of TryGetAttributeValue.

Can you explain the difference between when you should use GetValueAsObject from an attribute vs TryGetAttributeValue from an iteminfo?
 
Sure

TryGetAttribute returns the AttributeBase object. this object has references to the item/definition/category, it's parent (inherited value), override value, bound value, etc.... you can use "attribute.GetValueAsObject()" to get the value of the attribute as an "object" type. You can even do "attribute.SetOverrideValueAsObject(newValue)" to set the value at runtime without knowing the type (only works on Item attributes and if the type matches, otherwise it gives an error).
You can do many things without even knowing the type of the attribute.

If you knew the type of the attribute then you could use the "item.GetAttribute<Attribute<MyType>>(...)" function. That would return an Attribute<MyType> object (Attribute<T> inherits from AttributeBase). From there you could get the value using "attribute.GetValue()" and the would return a MyType value.

You would use TryGetAttribute when you do not know the type of the attribute. For example that's the case in the valuetocoloritemview script, where I try to parse the value of an unknown type as an index.

TryGetAttributeValue returns directly the value of the attribute if it exists. So if you have an "int" attribute it will return an "int" compared to GetAttribute which will return an "Attribute<int>". Of course you'll need to know that value type of your attribute in advance (which you usually do).

I hope that makes sense
 
Top