How to use the UserData property in DamageData?

Hi ,
in the DamageData class, I see there is a "UserData" property. Firstly, I want to say I really like how this is all engineered to be user extendable without needing to copy all the scripts or modifying the source. Second... could someone recommend to me how to properly use the UserData field? I want to have a "UseColliderDamageMultiplier" boolean so certain weapons with a higher weakpoint damage multiplier can override the collider damage multiplier.

Obviously I won't be able to change the type of UserData to one of my own classes, as they exist outside of the Opsive namespace and assembly... nor can I have the DamageData instantiate a new class of my own and assign it to UserData on construction due to the same constraint.

So, what to do?

Currently, I have added a method directly to DamageData (which will be erased if I pull any updates for UCC). It is as follows

C#:
public virtual T GetUserData<T>() where T : class, new()
{
T returnVal = UserData as T;
if (returnVal == null)
{
returnVal = new T();
UserData = returnVal;
}
return returnVal;
}

which can be called by my other user defined scripts.
C#:
damageData.GetUserData<MyExtraDamageDataFieldsClass>().IsThisFireDamageOrWhatever;

I was unable to find documentation on the proper usage of UserData in DamageData, and would like input on my solution/ pointed in the direction of some documentation if I missed it.

Pinging @Sangemdoko as instructed. :)
 
I think your use case makes sense to use the UserData of the DamageData.

The DamageData is created by the the "SimpleDamage" of the "ImpactActions". So you could make a subclass of DamageData and replace the "SimpleDamage" module by a custom one to use your custom damage data if you want to. Or you can use the UserData if you want.

If you prefer using the user data like you are doing right now without having to worry about getting overwritten by an update, you can write your function above as a static extension method:

I think writing your own custom ImpactAction is the way to go, it will give you a ton of flexibility as you'll have all the data from the ImpactCallbackContext to get all the information about the source and target to compute the damage however you want to.
 
Thanks for the feedback! I didn't think of making it an extension method. That is a much better idea.
I will also create my own impactAction for damage extending (or copying) SimpleDamage.
I think continuing to use Opsive DamageData is the way to go, at least until I have a much better understanding of how the parts UCC are interacting with eachother.

Thanks!

For anyone interested, here is my extension class:

C#:
using Opsive.UltimateCharacterController.Traits.Damage;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MyCompany.MyGame
{
    public static class OpsiveDamageDataExtensions
    {
        public static T GetUserData<T>(this DamageData damageData) where T : class, new()
        {
            T returnVal = damageData.UserData as T;

            if (returnVal == null)
            {
                returnVal = new T();
                damageData.UserData = returnVal;
            }

            return returnVal;
        }
    }
}
 
Top