Forum rules - please read before posting.

Creating Custom Action With Parameter

Trying to learn the scripting side of AC, I've done a simple custom Action that simply just adds 1 Strength:

Code:

[System.Serializable]
public class AddStrength : Action
{

    // Declare variables here


    public AddStrength()
    {
        this.isDisplayed = true;
        category = ActionCategory.Custom;
        title = "Add Strength";
        description = "Test for adding Strength";
    }


    public override float Run ()
    {
        AC.GlobalVariables.GetVariable("Strength").IntegerValue += 1;

        return 0f;
    }


    public override void Skip ()
    {
         Run ();
    }


    #if UNITY_EDITOR

    public override void ShowGUI ()
    {
        // Action-specific Inspector GUI code here

        AfterRunningOption ();
    }


    public override string SetLabel ()
    {
        // (Optional) Return a string used to describe the specific action's job.

        return string.Empty;
    }

    #endif

}

Now I wanna do the same thing except with parameters, so I can just enter a value within the parameter to determine how much of a strength I want when using the Editor. Looking at the ActionParameter script, there seems to be a lot of code, I'm not sure if all are necessary or only some. So I would appreciate it if anyone could show me how the above code would work with a parameter.

Thank you!

Comments

  • Isn't this already possible by using the Action type Variable/Set and the method Increase By Value?

  • Using the editor yeah it is, but I'm trying to understand the scripting side, so that I may learn how to create my own custom actions with its own logic etc.

  • edited August 2020

    Are you referring to "ActionList parameters" - which allow you to override Action field values - or are you just looking to set the value within the Action itself?

    Simply exposing the value in the Action itself just needs you to store that value as a public variable, display a field for it in the ShowGUI function, and then use it in the IntegerValue setter.

    Also, if your Skip and SetLabel functions don't override anything, they can be safely removed:

    using UnityEngine;
    using System.Collections.Generic;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
    
        [System.Serializable]
        public class AddStrength : Action
        {
    
            public int strengthIncrease = 1;
    
            public AddStrength()
            {
                this.isDisplayed = true;
                category = ActionCategory.Custom;
                title = "Add Strength";
                description = "Test for adding Strength";
            }
    
            public override float Run ()
            {
                AC.GlobalVariables.GetVariable("Strength").IntegerValue += strengthIncrease;
                return 0f;
            }
    
            #if UNITY_EDITOR
    
            public override void ShowGUI ()
            {
                strengthIncrease = EditorGUILayout.IntField ("Increase by:", strengthIncrease);
    
                AfterRunningOption ();
            }
    
            #endif
    
        }
    
    }
    

    If you wanted to also be able to override this field with an Integer parameter (see a tutorial on parameters here), then you can do so by creating another integer variable to store that parameter's ID, and use it to override your set value if non-negative:

    using UnityEngine;
    using System.Collections.Generic;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
    
        [System.Serializable]
        public class AddStrength : Action
        {
    
            public int strengthIncrease = 1;
            public int parameterID = -1;
    
            public AddStrength()
            {
                this.isDisplayed = true;
                category = ActionCategory.Custom;
                title = "Add Strength";
                description = "Test for adding Strength";
            }
    
            public override void AssignValues(List<ActionParameter> parameters)
            {
                strengthIncrease = AssignInteger (parameters, parameterID, strengthIncrease);
            }
    
            public override float Run ()
            {
                AC.GlobalVariables.GetVariable("Strength").IntegerValue += strengthIncrease;
                return 0f;
            }
    
            #if UNITY_EDITOR
    
            public override void ShowGUI (List<ActionParameter> parameters)
            {
                parameterID = Action.ChooseParameterGUI ("Increse by:", parameters, parameterID, ParameterType.Integer);
                if (parameterID < 0)
                {
                    strengthIncrease = EditorGUILayout.IntField ("Increase by:", strengthIncrease);
                }
    
                AfterRunningOption ();
            }
    
            #endif
    
        }
    
    }
    

    A tutorial on writing custom Actions that use ActionList parameters can be found here.

  • Thank you thats what I needed, and yes I was referring to Actionlist parameters

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.