Adventure Creator Wikia
Advertisement

This is a Custom Action that is used to check a unlimited number of global boolean variables if they meet given conditions. I use it to clean up my ActionLists if there are to many variable state checks in a row.

To describe it as simple as possible:

It can turn this

MultiBoolCheck

into this:

MultiBoolCheck2

Important to know:

- It only works with global boolean variables

- It does not support ActionList parameters

It is based on the "Variable: Check" Action. Here's the code:

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

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace AC
{

	[System.Serializable]
	public class MultiBool
    {

	    public int variableID;
		public BoolValue boolValue = BoolValue.True;

    }

	public class ActionMultiBoolCheck : ActionCheck
	{

		public int NumberOfBools = 1;
		public int variableNumber;
		public List<MultiBool> multiBool=new List<MultiBool>();

		public override ActionCategory Category { get { return ActionCategory.Variable; } }
		public override string Title { get { return "MultiBoolCheck"; } }
		public override string Description { get { return "Queries the value of one or more Global Bool Variables declared in the Variables Manager."; } }

		public override bool CheckCondition ()
		{
			int Hits = 0;
			int Count=0;

			foreach (MultiBool _multiBool in multiBool)
			{
				if (_multiBool.variableID == -1)
				{
					ACDebug.LogWarning ("The 'Variable: MultiBoolCheck' Action halted the ActionList because it cannot find a given Global Variable (Index="+Count+")");
					return false;
				}

				GVar var = GlobalVariables.GetVariable (_multiBool.variableID);
				if (var != null)
				{
					var.Download ();
					if (var.BooleanValue == true && _multiBool.boolValue == BoolValue.True)
					{
						Hits++;
					}
					else if (var.BooleanValue == false && _multiBool.boolValue == BoolValue.False)
					{
						Hits++;
					}
				}
				else
				{
					ACDebug.LogWarning ("The 'Variable: MultiBoolCheck' Action halted the ActionList because it cannot find the Global Variable with an ID of " + _multiBool.variableID);
					return false;
				}
				Count++;
			}

			if (Hits == NumberOfBools)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		#if UNITY_EDITOR

		override public void ShowGUI (List<ActionParameter> parameters)
		{
			int OldNumber = NumberOfBools;

			NumberOfBools = EditorGUILayout.IntField ("Number of checks:", NumberOfBools);

			if (NumberOfBools < 1)
			{
				NumberOfBools = 1;
			}
		
			// If "Number of checks" is altered then delete or create multiBool indexes accordingly
			int Dif = OldNumber - NumberOfBools;
			if (Dif > 0)
			{
			   multiBool.RemoveRange(NumberOfBools,Dif);
			}
			else if (Dif < 0)
			{
				for (int Count=0; Count < -Dif; Count++)
				{
 			  		multiBool.Add(new MultiBool());
     			}
			}

			EditorGUILayout.LabelField (" ", GUILayout.MaxWidth (60f));
			VariablesManager variablesManager = AdvGame.GetReferences ().variablesManager;
		
			foreach (MultiBool _multiBool in multiBool)
			{
				_multiBool.variableID = ShowVarGUI (variablesManager.vars, _multiBool.variableID, true);
				_multiBool.boolValue = (BoolValue) EditorGUILayout.EnumPopup ("has to be:",_multiBool.boolValue);
				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.Boolean)
				{
    				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 " ("+NumberOfBools+" 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.Boolean)
				{
					i++;
				}
			}
			return -1;
		}

		private int GetVarID (List<GVar> vars, int Number)
		{
			int i = 0;

			foreach (GVar _var in vars)
			{
				if (_var.type == VariableType.Boolean)
				{
					if (i == Number)
					{
						return _var.id;
					}
					i++;
				}
			}
			return -1;
		}

	}

}
Advertisement