Page 1 of 1

Custom SceneEventQuestAction

Posted: Fri Dec 01, 2023 5:08 am
by danieltorok
Hello! I'm wondering if it would be possible to create a custom SceneEventQuestAction, that doesn't just contain the onExecute UnityEvent field. I tried to modify the QuestMachineSceneEvent class to include more fields, but they are not showing in the editor ui.

Re: Custom SceneEventQuestAction

Posted: Fri Dec 01, 2023 8:03 am
by Tony Li
Hi,

You can create your own quest actions -- no need to modify existing ones. Duplicate QuestActionTemplate.cs, move it into your own scripts folder, and fill in the code where indicated.

Re: Custom SceneEventQuestAction

Posted: Fri Dec 01, 2023 8:34 am
by danieltorok
Hello,

That's what I did first time but I noticed that I cannot drag and drop scene objects into the declared fields. I suppose it's a limitation of how these classes are handled, and thats why there is a separate SceneEventQuestAction.

So my question is would it be possible to create custom scene event quest actions other than the default?

Re: Custom SceneEventQuestAction

Posted: Fri Dec 01, 2023 10:21 am
by Tony Li
Sure! Here's an example subclass that also logs a message to the Console when it runs the quest action:

CustomSceneEventQuestAction.cs

Code: Select all

using UnityEngine;
namespace PixelCrushers.QuestMachine
{
    public class CustomSceneEventQuestAction : SceneEventQuestAction
    {
        [SerializeField] private string m_logMessage;
        public string logMessage => m_logMessage;

        public override void Execute()
        {
            base.Execute();
            Debug.Log(logMessage);
        }
    }
}
And in an Editor folder:

CustomSceneEventQuestActionEditor.cs

Code: Select all

using UnityEditor;
namespace PixelCrushers.QuestMachine
{
    [CustomEditor(typeof(CustomSceneEventQuestAction), true)]
    public class CustomSceneEventQuestActionEditor : SceneEventQuestActionEditor
    {
        protected override void Draw()
        {
            base.Draw();
            if (serializedObject == null) return;
            serializedObject.Update();
            EditorGUILayout.PropertyField(serializedObject.FindProperty("m_logMessage"), true);
            serializedObject.ApplyModifiedProperties();
        }
    }
}

Re: Custom SceneEventQuestAction

Posted: Fri Dec 01, 2023 11:17 am
by danieltorok
Hi,

Thank you, I was missing the Editor folder part. Just for completeness sake, this is what I needed to include to be able to drag-and-drop a scene game object:

Code: Select all

var t = target as Custom;
t.gameObject = (GameObject) EditorGUILayout.ObjectField(t.gameObject, typeof(GameObject), true);
Also, placing the editor script in the Editor folder is not optional - I did not know that.

Thanks for the help!

Re: Custom SceneEventQuestAction

Posted: Fri Dec 01, 2023 11:31 am
by Tony Li
Glad to help!

Yes, any scripts that add custom Unity editor functionality must be inside an Editor folder hierarchy.

Related, if any scripts that are outside of an Editor folder reference any Unity editor namespaces (e.g., "using UnityEditor;"), Unity won't be able to build the project to an executable. Just for future reference.