I wanted to do change the onEnterAction and onExitAction to no longer be strings but instead be a Triggerable object. I created an interface called ITriggerable. I then went to change the DialogueNode as follows but it doesn’t serialize in the inspector. It seems this is a known limitation of Unity. [SeralizeReference] doesn’t work. I tried searching many forums but couldn’t understand some of the recommendations.
My fallback plan is to take in a UnityEngine.Object and then in OnValidate reject anything that doesn’t implement ITriggerable.
public class DialogueNode : ScriptableObject
{
[SerializeField] ITriggerable onEnterAction;
[SerializeField] ITriggerable onExitAction;
}
namespace RPG.Core
{
public interface ITriggerable
{
public void Trigger();
}
}
To see what I want to do and why I need an interface here are other relevant parts of the code. As you can see I want things that inherit from MonoBehaviors and ScriptableObjects to implement ITriggerable. So this would allow me to
- trigger a quest to start
- trigger a quest objective to be completed
- trigger an aggro group to activate
namespace RPG.Dialogue
{
public class PlayerConversant : MonoBehaviour {
private void TriggerEnterAction()
{
if (currentNode != null)
{
TriggerAction(currentNode.GetOnEnterAction());
}
}
private void TriggerExitAction()
{
if (currentNode != null)
{
TriggerAction(currentNode.GetOnExitAction());
}
}
private void TriggerAction(ITriggerable action)
{
if (action == null) return;
action.Trigger();
}
}
}
using RPG.Core;
using UnityEngine;
namespace RPG.Combat
{
public class AggroGroup : MonoBehaviour, ITriggerable
{
public void Trigger()
{
Activate(true);
}
}
}
using RPG.Core;
using UnityEngine;
namespace RPG.Quests
{
[CreateAssetMenu(fileName = "QuestCompletion", menuName = "RPG/Quest/Completion", order = 0)]
public class QuestCompletion : ScriptableObject, ITriggerable
{
[SerializeField] Quest quest;
[SerializeField] string objective;
public void Trigger()
{
QuestList questList = GameObject.FindGameObjectWithTag("Player").GetComponent<QuestList>();
questList.CompleteObjective(quest, objective);
}
}
}
public class Quest : ScriptableObject, ITriggerable
{
public void Trigger()
{
QuestList questList = GameObject.FindGameObjectWithTag("Player").GetComponent<QuestList>();
questList.AddQuest(this);
}
}