Forum rules - please read before posting.

Custom Actionlist - How to use arrays

I been making some custom actionlist of my own and they all have been working well until I came across actions that would require array of data and I just don't know how its implemented.

I would like the array to be displayed as textfields just like how it would show on the unity inspector, giving you a plus and minus sign to add another field to the array or remove an element from array.

In the below example I'm trying to create an actionlist working with array where I'm simply going through my array of textfields and inputting a bunch of messages and then trying to display those messages that I have inputted. However this doesn't work and I would really appreciate any help on how something like this can be implemented.

Here is an example

    namespace AC
    {
        [System.Serializable]
        public class ArrayTestExample : Action
        {
            public string[] messages;
            public int parameterID = -1;

            public override ActionCategory Category { get { return ActionCategory.Custom; } }
            public override string Title { get { return "Test title"; } }
            public override string Description { get { return "Test Desc"; } }
            public override int NumSockets { get { return 1; } }

            public override float Run()
            {
                for (int i = 0; i < messages.Length; i++)
                {
                    Debug.Log(messages[i]);
                }
                return 0f;

            }

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

    #if UNITY_EDITOR

            public override void ShowGUI(List<ActionParameter> parameters)
            {
                parameterID = Action.ChooseParameterGUI("Test", parameters, parameterID, ParameterType.Integer);
                if (parameterID < 0)
                {
                    for (int i = 0; i < messages.Length; i++)
                    {
                        messages[i] = EditorGUILayout.TextField("Message: ", messages[i]);

                    }
                }

                AfterRunningOption();
            }

    #endif

        }
    }

Admittedly I am also quite new when it comes to working with making your own custom editor and using EditorGUILayout to design/draw your own UI etc.

Comments

  • When rendering fields in a custom Editor window, as Actions do, the behaviour of arrays has to be hard-coded. You need to expose a means of setting the size of the array.

    As doing so will also clear all existing values, you also need to backup the array beforehand, so that you can re-apply the values into the new array:

    using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    using System.Collections.Generic;
    
    namespace AC
    {
        [System.Serializable]
        public class ArrayTestExample : Action
        {
            public string[] messages = new string[0];
    
            public override ActionCategory Category { get { return ActionCategory.Custom; } }
            public override string Title { get { return "Test title"; } }
            public override string Description { get { return "Test Desc"; } }
            public override int NumSockets { get { return 1; } }
    
            public override float Run()
            {
                for (int i = 0; i < messages.Length; i++)
                {
                    Debug.Log(messages[i]);
                }
                return 0f;
    
            }
    
            public override void Skip()
            {
                Run();
            }
    
            #if UNITY_EDITOR
    
            public override void ShowGUI(List<ActionParameter> parameters)
            {
                int numMessages = messages.Length;
                numMessages = EditorGUILayout.DelayedIntField ("# of messages:", numMessages);
    
                if (numMessages != messages.Length)
                {
                    // Rebuild array, back it up first
                    string[] backup = new string[messages.Length];
                    for (int i = 0; i < messages.Length; i++) backup[i] = messages[i];
    
                    // Resize the array
                    messages = new string[numMessages];
    
                    // Restore the backup
                    for (int i = 0; i < messages.Length; i++)
                    {
                        if (i < backup.Length)
                        {
                            messages[i] = backup[i];
                        }
                    }
                }
    
                for (int i = 0; i < messages.Length; i++)
                {
                    messages[i] = EditorGUILayout.TextField("Message: ", messages[i]);
                }
            }
    
            #endif
    
        }
    }
    
  • Oh I see, I understand now. Thank you so much for this, now I have a good understanding of how array would work for actionlists.

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.