playMaker

Author Topic: Custom Action Editor [SOLVED]  (Read 3224 times)

HAIRGROW

  • Playmaker Newbie
  • *
  • Posts: 24
Custom Action Editor [SOLVED]
« on: May 29, 2018, 02:09:19 AM »
So I'm in the process of creating a custom action and I wanted to know if its possible to create a single action from multiple script files?

First, let me say that I'm an artist who's still learning C#, but I understand all the basics. Enough to be able to successfully write a single working custom action. The action that I'm writing is comprised of several "settings" such as check boxes, public variables, and drop drown menus. I've got each section of the action working separately (as it own "Action" for testing purposes), but instead of writing one long gigantic script file for the whole action, which would ultimately work, I would like to break it down in to smaller chunks of code (classes) that then get compiled together via the Custom Action Editor.

After studying the Quaternion Actions, I saw how each action was a child to the parent class QuaternionActionBase. Then, each action, as well as QuaternionActionBase, has it's own Custom Action Editor script, which are all children of the QuaternionCustomEditorBase class. So, with the concept of Inheritance in mind and to prevent my script from becoming massively long, I want to know if it's possible to write the entire action in separate classes. So that way I can focus on each section of the code, get it working, and then use Action Editor to arrange all the fields.

As I'm learning more about the Custom Action Editor, I'm wondering if I could write each section of the action as a separate script file, with each script using it's on custom editor, and then create a parent editor that bring all the children (fields) together in one action. Is this possible or am I going about this the wrong way?

What I've discovered thus far, is that the Action Editor only works on the script that it's pointed to. So for the Quaternions for example, GetQuaternionEulerAnglesCustomEditor.cs , will only work on GetQuaternionEulerAngles.cs, because of this line here:

[CustomActionEditor(typeof(GetQuaternionEulerAngles))]

If that's the case, will I have to suck it up and put everything into the main action and utilize 1 Custom Editor Script for the action?

Like I said, I'm learning more and more about C# and what's possible so it would awesome to create smaller modules that I could then bring all together.

Thanks in advance for any advice given.

Here's an example of what I'm trying to do

Code: [Select]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// This is the parent class. It's only job is to establish the public gameobject variable.
/// All subsequent scripts will be derived from this class, so they all will have access to the parent game object.
/// </summary>

namespace HutongGames.PlayMaker.Actions
{

    public abstract class SampleActionBase : FsmStateAction
    {
        [RequiredField]
        [Tooltip("The Game Object")]
        [CheckForComponent(typeof(GameObject))]
        public FsmOwnerDefault gameObject;
        public GameObject go;


    }
}

Code: [Select]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// This is a child to SampleActionBase.
/// It would contain most of the settings,while the rest comes from another script.
/// This would be the script that actually shows up in the Action Browser
/// </summary>

namespace HutongGames.PlayMaker.Actions
{

    [ActionCategory("Get Some")]
    [Tooltip("Tips! Tips! Tips!")]

    public class SampleAction : SampleActionBase
    {

        public FsmFloat time;
        public FsmBool looping;


        public override void Reset()
        {

            time = null;
            looping = true;
        }

        public override void OnEnter()
        {

            DoStuff();
            Finish();

        }

        public void DoStuff()
        {
            go = Fsm.GetOwnerDefaultTarget(gameObject);

            // Then do some stuff with this


        }

    }


}

Code: [Select]
using UnityEngine;
using UnityEditor;
using HutongGames.PlayMaker.Actions;
using HutongGames.PlayMakerEditor;

/// <summary>
/// Not sure what to put here. With this being the parent I thought I could
/// expose public FsmOwnerDefault gameObject; here so all children the UI would show up in a child classes,
/// but then I figured out I didn't have to do that.
/// </summary>

public class SampleActionUIBase : CustomActionEditor
{

    public override bool OnGUI()
    {
        return false;
    }

}

Code: [Select]
using UnityEngine;
using UnityEditor;
using HutongGames.PlayMaker.Actions;
using HutongGames.PlayMakerEditor;

/// <summary>
/// This script controls the interface for the SampleAction script.
/// I'd like to add Edit Fields from other scripts in this file.
/// so I could organize each section of the UI.
/// </summary>

[CustomActionEditor(typeof(SampleAction))]
public class SampleActionUI : SampleActionUIBase
{
    StartDelayUI startDelayInst;

    public override bool OnGUI()
    {

        EditField("gameObject");
        EditField("time");
        EditField("looping");


        GUILayout.Label("Next Section");

        //I'd like to place EditFields from other script files here.
        //Thus giving me more control over the layout of the interface
        // And making it easier to manage all the code.
;

        return GUI.changed;
    }
}

« Last Edit: June 06, 2018, 11:13:29 PM by HAIRGROW »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Custom Action Editor
« Reply #1 on: May 30, 2018, 02:58:32 AM »
Hi,

 Yeah, you will be limited with hardcoded relationship between an action and its editor. I am not sure there is a way around this.

 but really, the way to go here is to work like I did for quaternions, and also create some abstracted methods and feature that you caall from the various actions and editor.

 check out my XmlMaker actions and editor, you'll see that I have to have each action and editor as individual scripts, and they call common methods and classes.

 Bye,

 Jean

HAIRGROW

  • Playmaker Newbie
  • *
  • Posts: 24
Re: Custom Action Editor
« Reply #2 on: May 30, 2018, 06:18:41 PM »
Thanks for the reply Jean. I actually came across XML Maker and was studying those as well. From my setup, is it possible to have a base action (that doesn't show up in the browser) establish the public game object in the parent FSMStateAction class, and then have derived classes (that don't show up in the browser as well) operate on that same game object? I assumed through the power of inheritance this would work, but for some reason I'm getting "object reference not set to an instance of an object" in the derived classes.

I'm still learning the more complex parts of C# but I think we're on the same page with your explanation. Essentially all the derived classes are siblings to the parent FSMStateAction class, but one of the siblings is the Main Action that shows up in the browser. The Main Actions public variables would send it's information to the variables and functions of it's siblings, which will do all the main work.

I thought about daisy chaining each section via inheritance such that the last child in the hierarchy would be the Main Action that has access to all of it's parents variables. But now that I'm getting the object reference error, even though I'm using derived classes, I'm not sure daisy chaining would work. And then, I don't want to string them all together in such a linear fashion, just in case I need to make a change to one of the parents, which would ultimately effect its children.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Custom Action Editor
« Reply #3 on: May 31, 2018, 02:08:06 AM »
Hi,

 yeah, chaining isn't going to work... I think the approach I took for xml actions is the most pragmatic approach, I'd like some c# guru to step in some time, maybe there are things to improve in the way we can design a complex set actions with minimal code and maximal maintainability...

 Bye,

 Jean

HAIRGROW

  • Playmaker Newbie
  • *
  • Posts: 24
Re: Custom Action Editor
« Reply #4 on: May 31, 2018, 01:57:36 PM »
So I'm still trying to find my answer by looking at the xml actions, but because it's not storing a game object with
Code: [Select]
public FsmOwnerDefault gameObject I can't find what I need.

So if daisy chaining doesn't work, does that mean I can't reference the public game object from a base class in derived classes? Right now I'm getting the "Object reference not set to an instance of an object" error in my derived class.

I just want to know before I start creating one monolithic script.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Custom Action Editor
« Reply #5 on: June 01, 2018, 02:49:30 AM »
Hi,

 without the context in which you are using this, I can't say what's wrong. could you wrap up a sample so that I can replicate here?

 Bye,

 Jean

HAIRGROW

  • Playmaker Newbie
  • *
  • Posts: 24
Re: Custom Action Editor
« Reply #6 on: June 02, 2018, 04:58:26 PM »
Do you have a direct email I could send it to?

HAIRGROW

  • Playmaker Newbie
  • *
  • Posts: 24
Re: Custom Action Editor [SOLVED]
« Reply #7 on: June 06, 2018, 11:12:59 PM »
After much trial and error and I finally solved my problem. I had to create a reference to other classes in my main FSMStateAction script. And then in OnEnter I call those functions with the gameobject as one of the arguments. This is great because I seriously didn't want to write one gi-normous script file for one action. Now I can off load portions of it to as many c# scripts as I want.