Custom SceneEventQuestAction

Announcements, support questions, and discussion for Quest Machine.
Post Reply
danieltorok
Posts: 11
Joined: Tue Nov 28, 2023 9:36 am

Custom SceneEventQuestAction

Post 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.
User avatar
Tony Li
Posts: 22104
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom SceneEventQuestAction

Post 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.
danieltorok
Posts: 11
Joined: Tue Nov 28, 2023 9:36 am

Re: Custom SceneEventQuestAction

Post 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?
User avatar
Tony Li
Posts: 22104
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom SceneEventQuestAction

Post 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();
        }
    }
}
danieltorok
Posts: 11
Joined: Tue Nov 28, 2023 9:36 am

Re: Custom SceneEventQuestAction

Post 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!
User avatar
Tony Li
Posts: 22104
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom SceneEventQuestAction

Post 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.
Post Reply