In unity i have a scriptable object Card, which can have multiple effects. I want those effects to also be scriptable objects with some settings, but i want to be able to override those effect's attributes if needed when i add them to a card. For example i want to create a "Poison" scriptable object effect, then add it to the card and then adjust the "duration" or "damage" of it in it. How do i do it and how do i display it properly in editor?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To achieve this, you can create a base ScriptableObject for the effects, and then create a derived ScriptableObject for each specific effect (e.g., Poison). You can then use a dictionary or list to store the base effects and their override values. Here's an example:
Create a base ScriptableObject for the effects:
c#
[CreateAssetMenu(menuName = "ScriptableObjects/CardEffect")]
public class CardEffectSO : ScriptableObject
{
public string effectName;
public abstract class Effect
{
public virtual void ApplyEffect(GameObject target) { }
}
public List<Effect> effectList = new List<Effect>();
}
Create a derived ScriptableObject for each specific effect:
c#
[CreateAssetMenu(menuName = "ScriptableObjects/PoisonEffect")]
public class PoisonEffectSO : CardEffectSO
{
[Tooltip("Duration of the poison effect")]
public float duration;
[Tooltip("Amount of damage caused by the poison effect")]
public int damage;
public override void ApplyEffect(GameObject target)
{
// Apply the Poison effect logic here
}
}
In your Card script, create a dictionary or list to store the base effects and their override values:
c#
[SerializeField]
private CardEffectSO _baseEffects;
[SerializeField]
private PoisonEffectSO _poisonEffect;
private Dictionary<string, float> _effectOverrides = new Dictionary<string, float>();
private void Awake()
{
if (_baseEffects == null) _baseEffects = new CardEffectSO();
if (_poisonEffect == null) _poisonEffect = new PoisonEffectSO();
// Initialize the effect overrides
_effectOverrides.Clear();
_effectOverrides.Add(_poisonEffect.effectName, _poisonEffect.duration);
}
public void AddEffect(CardEffectSO effect)
{
// Add the effect to the base effects list
_baseEffects.effectList.Add(effect);
}
public void OverrideEffect(string effectName, float overrideValue)
{
// Add the override value to the dictionary
_effectOverrides[effectName] = overrideValue;
}
public void ApplyEffects(GameObject target)
{
// Apply each effect in the base effects list with their respective overrides
foreach (var effect in _baseEffects.effectList)
{
effect.ApplyEffect(target);
if (_effectOverrides.ContainsKey(effect.effectName))
{
// Apply the override value if it exists
switch (effect)
{
case PoisonEffectSO poisonEffect:
poisonEffect.duration = _effectOverrides[effect.effectName];
break;
// Add cases for other derived effects as needed
default:
break;
}
}
}
}
To display the effects in the editor, you can create an Editor class for the Card script:
c#
[CustomEditor(typeof(Card))]
public class CardEditor : Editor
{
public override void OnInspectorGUI()
{
var card = target as Card;
base.OnInspectorGUI();
if (GUILayout.Button("Add Poison Effect"))
{
var poisonEffect = ScriptableObject.CreateInstance<PoisonEffectSO>();
poisonEffect.duration = 5f;
poisonEffect.damage = 10;
// Add the poison effect to the card's base effects list
card._baseEffects.effectList.Add(poisonEffect);
AssetDatabase.CreateAsset(poisonEffect, "Assets/PoisonEffectSO.asset");
AssetDatabase.SaveAssets();
}
}
}
This example demonstrates how to use a dictionary to store the effect overrides. If you prefer to use a list, you can modify the code accordingly.