Forum rules - please read before posting.

DoOnce script

Hello,
Another developer has created a "DoOnce" script for AC, to easily make sure an action is only carried out once.

However, it was made for an earlier version of AC and doesn't compile with my version.

Here's the script:
https://drive.google.com/file/d/1awTNc8MLEWy-SuBrIo8drLwLXve2XmpG/view?usp=sharing

When I try to run it, I get lots of warnings, most of them saying things like "Action.category is not a property" and "Action.title is not a property."

I can show a complete log, but I suspect you might be able to quickly see why it won't work, and if there's an easy workaround. I assume something about how actions are hardcoded in AC has changed?

Comments

  • The Action API was updated in v1.73 - if a custom Action was written before then, it'll need to be updated following these guidelines.

    This may not work 100% as-intended as I'm not entirely sure about its intent, but this will at least fix the compilation errors:

    /*
     *
     *  
     *  JSH
     *  
     *  "ActionDoOnce.cs"
     * 
     *  Runs an action only once.
     *  
     * 
     */
    
    using UnityEngine;
    using System.Collections.Generic;
    using UnityEngine.Assertions;
    #if UNITY_EDITOR
    using UnityEditor;
    using UnityEditor.SceneManagement;
    #endif
    
    namespace AC
    {
    
        [System.Serializable]
        public class ActionDoOnce : Action
        {
            public int variableID;
            protected GVar runtimeVariable;
            private bool bIsSkipping = false;
            public int numSockets = 2;
    
    
            public override ActionCategory Category { get { return ActionCategory.Variable; }}
            public override string Title { get { return "Do Once"; }}
            public override string Description { get { return "Runs an action only once.";}}
            public override int NumSockets { get { return numSockets; }}
    
    
            override public void AssignValues(List<ActionParameter> parameters)
            {
                runtimeVariable = null;
    
                if (!isAssetFile)
                    runtimeVariable = LocalVariables.GetVariable(variableID, KickStarter.localVariables);
    
                base.AssignValues(parameters);
            }
    #if UNITY_EDITOR
            protected override string GetSocketLabel(int i)
            {
                if (i == 1)
                    return "Execute once:";
                else
                    return "Execute subsequent:";
            }
    #endif
    
            public override void Skip()
            {
                bIsSkipping = true;
                /*Debug.LogError("ERROR: DoOnce found in skippable action list!");
                Assert.IsTrue(false, "ERROR: DoOnce found in skippable action list!");
                base.Skip();*/
            }
    
            public override float Run()
            {
                bIsSkipping = false;
                return 0f;
            }
    
            public override int GetNextOutputIndex ()
            {
                if (variableID == -1)
                {
                    return -1;
                }
    
                if (runtimeVariable != null)
                {
                    if (runtimeVariable.type == VariableType.Integer)
                    {
                        if (bIsSkipping)
                            runtimeVariable.RestoreBackupValue();
    
                        int originalValue = runtimeVariable.IntegerValue;
    
                        runtimeVariable.IntegerValue = 1;
    
                        if (bIsSkipping)
                            runtimeVariable.BackupValue();
    
                        runtimeVariable.Upload(VariableLocation.Local);
    
                        return originalValue;
                    }
                    else
                    {
                        LogWarning("'Variable: Run sequence' Action is referencing a Variable that does not exist or is not an Integer!");
                    }
                }
    
                return -1;
            }
    
    #if UNITY_EDITOR
    
            private int[] GetIDArray(List<GVar> _vars)
            {
                // 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();
            }
    
            override public void ShowGUI(List<ActionParameter> parameters)
            {
                if (KickStarter.localVariables != null && !isAssetFile)
                {
                    EditorGUILayout.BeginHorizontal();
                    variableID = ShowVarGUI(variableID);
                    EditorGUILayout.EndHorizontal();
                }
    
                bool bSetOutputs = numSockets > endings.Count;
                if (bSetOutputs && endings.Count > 0)
                {
                    Undo.RecordObject(KickStarter.localVariables, "Create variable");
    
                    string DoOncePrefix = "DoOnce/";
                    int Index = 1;
    
                    foreach (GVar var in KickStarter.localVariables.localVars)
                    {
                        if(var.label.Contains(DoOncePrefix))
                        {
                            Index++;
                        }
                    }
    
                    GVar newVariable = new GVar(GetIDArray(KickStarter.localVariables.localVars));
                    newVariable.label = $"DoOnce/{Index}-{parentList.name}";
                    newVariable.type = VariableType.Integer;
    
                    KickStarter.localVariables.localVars.Add(newVariable);
                    ACDebug.Log("Created new local variable \"" + newVariable.label + "\"");
    
                    this.variableID = newVariable.id;
    
                    EditorUtility.SetDirty(KickStarter.variablesManager);
                    EditorUtility.SetDirty(KickStarter.localVariables);
                    EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
                    UnityVersionHandler.SaveScene();
                }
            }
    
            private void Callback(object obj)
            {
                switch (obj.ToString())
                {
                    case "AutoCreate":
                        AutoCreateVariableWindow.Init("DoOnce/New integer", VariableLocation.Local, VariableType.Integer, this);
                        break;
    
                    case "Show":
                        if (AdvGame.GetReferences() != null && AdvGame.GetReferences().variablesManager != null)
                        {
                            AdvGame.GetReferences().variablesManager.ShowVariable(variableID, VariableLocation.Local);
                        }
                        break;
                }
            }
    
    
            private int ShowVarGUI(int ID)
            {
                return AdvGame.LocalVariableGUI("Variable:", ID, VariableType.Integer);
            }
    
            public override int GetNumVariableReferences (VariableLocation _location, int varID, List<ActionParameter> parameters, Variables _variables = null, int variablesConstantID = 0)
            {
                int thisCount = 0;
    
                if (VariableLocation.Local == _location && variableID == varID)
                    thisCount++;
    
    
                thisCount += base.GetNumVariableReferences(_location, varID, parameters, _variables, variablesConstantID);
                return thisCount;
            }
    #endif
            /**
             * <summary>Creates a new instance of the 'Variable: Run sequence' Action</summary>
             * <param name = "numOutcomes">The number of different outcomes</param>
             * <param name = "doLoop">If True, the first outcome will be run the next time after the last outcome is run</param>
             * <returns>The generated Action</returns>
             */
            public static ActionDoOnce CreateNew(int numOutcomes, bool doLoop)
            {
                ActionDoOnce newAction = (ActionDoOnce)CreateInstance<ActionDoOnce>();
                newAction.numSockets = numOutcomes;
                return newAction;
            }
    
        }
    
    }
    

    Is it possible the Variable: Run sequence Action serves a similar purpose?

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.