Durability

Arlo

New member
Hi!
In the documentation it states that Item Attributes can be modified at runtime and so are used for stuff like "a Durability attribute that decreases for each strike of a weapon"

I created an Attribute called Durability under Weapon item category and set its variant as Modify. Weapon is set as Mutable too.

Now... how to handle the part where this attribute decreases for each strike? Actually I would need too variants, one that decreases Durability in every use (like for shootable weapons) and one that only decreases when hitting something (like for swords and such).

Any help would be MUCH appreciated : )
 
I created an Attribute called Durability under Weapon item category and set its variant as Modify. Weapon is set as Mutable too.

So first miscomception. "Modify" is not particularly what you are looking for. You can set the attribute to whatever you like ("Inherit", "Override", "Modify").
Because the way it works is that you will set it as "Override" at runtime and set your own value.

Second potential issue. Setting as Mutable is Correct, but you also need to set as "Unique" to ensure your item attribute value is unique to your item and not shared between all items with the same definition.

Now... how to handle the part where this attribute decreases for each strike? Actually I would need too variants, one that decreases Durability in every use (like for shootable weapons) and one that only decreases when hitting something (like for swords and such).

That is completely up to you :) You have full freedom.
I assume you have some custom scripts for firing shootable weapons and using melee weapons.
When you want to change the durability you can do like so:

(Note the code below is not tested, I might have made a typo somewhere)
C#:
if (!item.IsMutable) {
    // Immutable items cannot have their attribute set.
    return;
}

// Retrieves an durability attribute assuming it is float.
var durabilityAttribute = item.GetAttribute<Attribute<float>>("Durability");
if (durabilityAttribute != null) {
    var currentValue = durabilityAttribute.GetValue();
    var decreaseAmount = 0.1f;
    var newValue =currentValue-decreaseAmount;
    
    if(newValue <= 0){
        // Break the weapon? Remove it? It's up to you.
    }else{
        durabilityAttribute.SetOverrideValue(newValue);
    }
}

Check out this page for more details:

I hope that helps :)
 
Top