Forum rules - please read before posting.
Tech questions during the weekend? Drop in on the community Discord channel!

[Feature Request] Editor: Allow for creation of variables in bulk + raise char limit in inspector

My first request is rather about convenience and speed.

Very often, when I need new variables, I need not one or two, but several dozens of them (and all of them of one specific type). Hence it would be great to have an option to create them in bulk, e.g. via a popup asking me:

  • How many?
  • Which type?
  • If type=popup, with how many value slots? And shall all popup vars heave the same values prefilled?
  • Which initial value?
  • What labelname (+automatic incrementing number)?

My second request: variable's names seem to get truncated from the 28th character on which is not really a lot, especially when using a naming/structure/like/this. Maybe raise this value or make it optional altogether?

Comments

  • it would be great to have an option to create them in bulk

    Try this:

    using UnityEngine;
    using UnityEditor;
    using System.Collections.Generic;

    namespace AC
    {

    public class BulkCreateVarsWindow : EditorWindow
    {
    
        private VariableLocation location = VariableLocation.Global;
        private int numVars = 1;
        private string namePrefix;
        private Variables variables;
        private GVar newVar = null;
    
    
        [MenuItem ("Adventure Creator/Editors/Bulk-create/Variables", false, 2)]
        static void Init ()
        {
            BulkCreateVarsWindow window = (BulkCreateVarsWindow) EditorWindow.GetWindow (typeof (BulkCreateVarsWindow));
            window.Repaint ();
            window.titleContent.text = "Bulk-create Variables Editor";
        }
    
    
        private void OnGUI ()
        {
            location = (VariableLocation) EditorGUILayout.EnumPopup ("Location:", location);
            if (location == VariableLocation.Component)
            {
                variables = (Variables) EditorGUILayout.ObjectField ("Variables:", variables, typeof (Variables), true);
            }
    
            if (newVar == null) newVar = new GVar ();
    
            EditorGUILayout.Space ();
    
            numVars = EditorGUILayout.IntSlider ("# of new vars:", numVars, 1, 20);
            namePrefix = EditorGUILayout.TextField ("Name prefix", namePrefix);
            newVar.type = (VariableType) EditorGUILayout.EnumPopup ("Type:", newVar.type);
    
            switch (newVar.type)
            {
                case VariableType.Boolean:
                    bool val = (newVar.val == 1);
                    val = EditorGUILayout.Toggle ("Default value:", val);
                    newVar.val = (val) ? 1 : 0;
                    break;
    
                case VariableType.Float:
                    newVar.floatVal = EditorGUILayout.FloatField ("Default value:", newVar.floatVal);
                    break;
    
                case VariableType.Integer:
                    newVar.val = CustomGUILayout.IntField ("Default value:", newVar.val);
                    break;
    
                case VariableType.PopUp:
                    Object objectToRecord = null;
                    if (location == VariableLocation.Global) objectToRecord = KickStarter.variablesManager;
                    if (location == VariableLocation.Local) objectToRecord = KickStarter.localVariables;
                    if (location == VariableLocation.Component) objectToRecord = variables;
    
                    VariablesManager.ShowPopUpLabelsGUI (newVar, true);
    
                    if (newVar.GetNumPopUpValues () > 0)
                    {
                        string[] popUpLabels = newVar.GenerateEditorPopUpLabels ();
                        newVar.val = CustomGUILayout.Popup ("Default value:", newVar.val, popUpLabels);
                    }
                    else
                    {
                        newVar.val = 0;
                    }
    
                    if (newVar.popUpID <= 0)
                    {
                        newVar.canTranslate = CustomGUILayout.Toggle ("Values can be translated?", newVar.canTranslate);
                    }
                    break;
    
                case VariableType.String:
                    EditorGUILayout.BeginHorizontal ();
                    CustomGUILayout.LabelField ("Default value:", GUILayout.Width (140f));
                    EditorStyles.textField.wordWrap = true;
                    newVar.textVal = CustomGUILayout.TextArea (newVar.textVal, GUILayout.MaxWidth (800f));
                    EditorGUILayout.EndHorizontal ();
    
                    newVar.canTranslate = EditorGUILayout.Toggle ("Values can be translated?", newVar.canTranslate);
                    break;
    
                case VariableType.Vector3:
                    newVar.vector3Val = EditorGUILayout.Vector3Field ("Default value:", newVar.vector3Val);
                    break;
            }
    
            EditorGUILayout.Space ();
    
            if (GUILayout.Button ("Bulk-create"))
            {
                Create ();
            }
        }
    
    
        private void Create ()
        {
            if (location == VariableLocation.Global)
            {
                if (KickStarter.variablesManager == null) return;
                Undo.RecordObject (KickStarter.variablesManager, "Add " + location + " variables");
            }
            else if (location == VariableLocation.Local)
            {
                if (KickStarter.localVariables == null) return;
                Undo.RecordObject (KickStarter.localVariables, "Add " + location + " variables");
            }
            else if (location == VariableLocation.Component)
            {
                if (variables == null) return;
                Undo.RecordObject (variables, "Add " + location + " variables");
            }
    
            if (Vars == null) return;
    
            for (int i=0; i<numVars; i++)
            {
                GVar variable = new GVar (newVar);
                variable.AssignUniqueID (GetIDArray ());
                variable.label = namePrefix + "_" + i.ToString ();
                Vars.Add (variable);
            }
            Debug.Log (numVars + " new " + location + " variables created");
        }
    
    
        private List<GVar> Vars
        {
            get
            {
                switch (location)
                {
                    case VariableLocation.Global:
                        return KickStarter.variablesManager.vars;
    
                    case VariableLocation.Local:
                        return KickStarter.localVariables.localVars;
    
                    case VariableLocation.Component:
                        return variables.vars;
    
                    default:
                        return null;
                }
            }
        }
    
    
        private int[] GetIDArray ()
        {
            // Returns a list of id's in the list
    
            List<int> idArray = new List<int>();
    
            foreach (GVar variable in Vars)
            {
                idArray.Add (variable.id);
            }
    
            idArray.Sort ();
            return idArray.ToArray ();
        }
    
    }
    

    }

    Paste it into a script named BulkCreateVarsWindow and place it in an Asset folder named Editor. It should then be accessible from the AC top toolbar.

    variable's names seem to get truncated from the 28th character on which is not really a lot

    Where exactly are you experiencing this? In an Action? This is likely a down to Unity's built-in behaviour, which AC's Editor interface relies on.

  • edited February 14

    This. Is. Absolutely. Awesome!

    Chris, I don't know what to say – thank you so, so much! Not only is this working beautifully plus a massive timesaver, too, it also help avoiding mistakes that almost inevitably occur when you repeat stuff a zillion times …

    What a feature!!!

  • As regards No.2: it's in the inspector window, where the truncation happens even though there is a lot of available space left.

  • Thanks for the feedback, I'll consider incorporating this into the official release.

    As regards No.2: it's in the inspector window,

    Noted, thanks for the clarification. I'll look into this.

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.