This can cause boxing.

hanguang

New member
/// <summary>
/// Set the override value as an object. This can cause boxing.
/// </summary>
/// <param name="overrideValue">The new Override value.</param>
/// <param name="setVariantTypeToOverride">Set the variant type to override.</param>
/// <param name="reevaluate">Reevaluate the attribute value.</param>
public abstract void SetOverrideValueAsObject(object overrideValue, bool setVariantTypeToOverride = true, bool reevaluate = false);

While reading the code of AttributeBase, I'm confused about this.
Could you please explain?
@Sangemdoko
 
Yes absolutely.
In some cases you might not know the type of the attribute but still want to get that value.
One of these examples is to debug all the attribute values of an item.
Similarly there are times you when you wish to change the attribute type of an attribute. Example int -> float. But you may want to keep that value without knowing that it was previously an int. There are other use cases where you may want to set the value without knowing the type (it's rare but it happens)

That function is part of the AttributeBase class which is abstract and doesn't have a value type.
The Attribute<T> inherits AttributeBase. That's where you'll find the function without boxing.

If you do know the attribute type (which is most of the time) then you should use the SetOverrideValue<T>(...) function.
from an item you can get the Attribute<T> using:

Code:
var attackAttribute = item.GetAttribute<Attribute<int>>("Attack");

read this page for more examples on how to use attributes correctly:
 
Top