Forum rules - please read before posting.

[Custom Action] Variables: MultiBoolCheck

Hi,

after not coding in C# for at least 10 years I sat myself down yesterday to code my first custom action. I call it "MultiBoolCheck" and it is used to check a unlimited number of global boolean variables if they meet given conditions. I need this to clean up my action lists if there are to many variable state checks.

Or simply said, it can turn this:
image
into this:
image

The Action is based on the "Variable: Check" Action. It only works with global boolean variables and does not support ActionList parameters. Maybe someone else has a use for it.

Comments

  • Oops. I wanted to post the code here but it tells me "Body is 82371 characters too long.". Any ideas how to share the code?
  • I made up a wiki page for this.
  • Thanks for posting!  The page can be found here.
  • Thank you! :)
  • This is very useful indeed! Thank's :)
  • edited August 2018
    @MAC

    This does not work for me in Unity 2018.2
    I put the MultiBoolCheck.cs where the AC scripts are and nothing ...
    Anything changed in scripting for 2018 this file ?
  • Custom Actions must be correctly installed following the guidelines outlined in the Manual's "Custom Actions" chapter as well as this tutorial.
  • 1. "If it is, choose to another platform, such as PC, Mac & Linux Standalone temporarily." checked
    2. "Within the Custom Action scripts panel, click Set directory." checked

    I do not think it is something I did wrong. What else should I check ?
  • I found the problem: I put the cs file in the Actionlist folder instead of Actions. And this is why it was not finding my new Action :)
  • This is such an amazing script...I've used it a lot in my game, which has several puzzle pieces that need to be positioned correctly over markers.  It's a great way to check if all the pieces are in the right spot.

    Can anyone help me modify this script to check Integer variables instead of Boolean variables? (I know just enough C# to get me into trouble!)
  • @sonicjoy: This won't do it completely, but you'll have a good start by renaming all instances of MultiBool to MultiInt, BoolValue to int, and boolValue to intValue (all case-sensitive).

    From there, it should mostly be a case of correcting errors that then appear in the Console.
  • Thanks Chris! I'll give it a go.
  • I know it's been a while, but this is extremely useful... but I can't get it to work. It's telling me there's an error on line 97: ActionMultiBoolCheck.cs(97,35): error CS0115: 'ActionMultiBoolCheck.End(List)': no suitable method found to override

    Any ideas?

  • Welcome to the community, @midnightdojo.

    It's an old Action that uses the old Action API, deprecated with AC v1.73.

    A guide to changes made to the API can be found here, but I've attempted to update the wiki page with the corrections - have a try now and see if it works.

  • Thanks @ChrisIceBox. You're epic! I got it to work, there were only two minor issues that I fixed. Maybe you can update the wiki with them.

    line 51 we have:
    Hits++:
    I changed it to
    Hits++;

    Then on line 61 we have
    {
    ACDebug.LogWarning("The 'Variable: MultiBoolCheck' Action halted the ActionList because it cannot find the Global Variable with an ID of " + _multiBool.variableID);
    return GenerateStopActionEnd();
    }

    I changed it to:
    {
    ACDebug.LogWarning($"The 'Variable: MultiBoolCheck' Action halted the ActionList because it cannot find the Global Variable with an ID of {_multiBool.variableID}");
    GenerateStopActionEnd();
    return false;
    }

    It seems to be working now! Tested it out with a few variables. This is going to save me a ton of time.

  • Amended - thanks for the pointers.

  • Cheers! I'm mostly just a composer who is working on a click adventure, so I'm not that good at coding yet, but I was able to hack around until I figured out what worked. Anyway, thanks again. It's been extremely useful.

  • Chris any chance you could modify this for integer variables? I know you mentioned it's just a matter of renaming certain instances but I have very little experience with hunting down errors in the console. Would be amazing if we had it tested by you.

  • edited December 2023

    I'm not the script's original author, but an adapated "MultiIntCheck" script should be something along these lines:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
    
        [System.Serializable]
        public class MultiInt
        {
    
            public int variableID;
            public int intValue;
    
        }
    
        public class ActionMultiIntCheck : ActionCheck
        {
    
            public int NumberOfInts = 1;
            public int variableNumber;
            public List<MultiInt> multiInts=new List<MultiInt>();
    
            public override ActionCategory Category { get { return ActionCategory.Variable; } }
            public override string Title { get { return "MultiIntCheck"; } }
            public override string Description { get { return "Queries the value of one or more Global Integer Variables declared in the Variables Manager."; } }
    
            public override bool CheckCondition ()
            {
                int Hits = 0;
                int Count=0;
    
                foreach (MultiInt multiInt in multiInts)
                {
                    if (multiInt.variableID == -1)
                    {
                        ACDebug.LogWarning ("The 'Variable: MultiIntCheck' Action halted the ActionList because it cannot find a given Global Variable (Index="+Count+")");
                        return false;
                    }
    
                    GVar var = GlobalVariables.GetVariable (multiInt.variableID);
                    if (var != null)
                    {
                        var.Download ();
                        if (var.IntegerValue == multiInt.intValue)
                        {
                            Hits++;
                        }
                    }
                    else
                    {
                        ACDebug.LogWarning ("The 'Variable: MultiIntCheck' Action halted the ActionList because it cannot find the Global Variable with an ID of " + multiInt.variableID);
                        return false;
                    }
                    Count++;
                }
    
                if (Hits == NumberOfInts)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
    
    
            #if UNITY_EDITOR
    
            override public void ShowGUI (List<ActionParameter> parameters)
            {
                int OldNumber = NumberOfInts;
    
                NumberOfInts = EditorGUILayout.IntField ("Number of checks:", NumberOfInts);
    
                if (NumberOfInts < 1)
                {
                    NumberOfInts = 1;
                }
    
                // If "Number of checks" is altered then delete or create multiInts indexes accordingly
                int Dif = OldNumber - NumberOfInts;
                if (Dif > 0)
                {
                   multiInts.RemoveRange(NumberOfInts,Dif);
                }
                else if (Dif < 0)
                {
                    for (int Count=0; Count < -Dif; Count++)
                    {
                        multiInts.Add(new MultiInt());
                    }
                }
    
                EditorGUILayout.LabelField (" ", GUILayout.MaxWidth (60f));
                VariablesManager variablesManager = AdvGame.GetReferences ().variablesManager;
    
                foreach (MultiInt multiInt in multiInts)
                {
                    multiInt.variableID = ShowVarGUI (variablesManager.vars, multiInt.variableID, true);
                    multiInt.intValue = EditorGUILayout.IntField ("has to be:",multiInt.intValue);
                    EditorGUILayout.LabelField (" ", GUILayout.MaxWidth (60f));
                }
            }
    
            private int ShowVarSelectorGUI (List<GVar> vars, int ID)
            {
                variableNumber = -1;
                List<string> labelList = new List<string>();
                foreach (GVar _var in vars)
                {
                    if (_var.type == VariableType.Integer)
                    {
                        labelList.Add (_var.label);
                    }
                }
                variableNumber = GetVarNumber (vars, ID);
    
                if (variableNumber == -1)
                {
                    ACDebug.LogWarning ("Previously chosen variable no longer exists!");
                    variableNumber = 0;
                    ID = 0;
                }
    
                variableNumber = EditorGUILayout.Popup ("Variable:", variableNumber, labelList.ToArray());
                ID = GetVarID(vars, variableNumber);
                return ID;
            }
    
            private int ShowVarGUI (List<GVar> vars, int ID, bool changeID)
            {
                if (vars.Count > 0)
                {
                    if (changeID)
                    {
                        ID = ShowVarSelectorGUI (vars, ID);
                    }
                    variableNumber = Mathf.Min (variableNumber, vars.Count-1);
                }
                else
                {
                    EditorGUILayout.HelpBox ("No variables exist!", MessageType.Info);
                    ID = -1;
                    variableNumber = -1;
                }
                return ID;
            }
    
            override public string SetLabel ()
            {
                return " ("+NumberOfInts+" checks)";
            }
    
            #endif
    
            private int GetVarNumber (List<GVar> vars, int ID)
            {
                int i = 0;
                foreach (GVar _var in vars)
                {
                    if (_var.id == ID)
                    {
                        return i;
                    }
    
                    if (_var.type == VariableType.Integer)
                    {
                        i++;
                    }
                }
                return -1;
            }
    
            private int GetVarID (List<GVar> vars, int Number)
            {
                int i = 0;
    
                foreach (GVar _var in vars)
                {
                    if (_var.type == VariableType.Integer)
                    {
                        if (i == Number)
                        {
                            return _var.id;
                        }
                        i++;
                    }
                }
                return -1;
            }
    
        }
    
    }
    
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.