FANDOM


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:

/*
*
* Adventure Creator
* by Chris Burton, 2013-2016
* "ActionVarCheck.cs" altered by Marcus 'MAC' Röckendorf in January 2017
*
* "ActionMultiBoolCheck.cs"
*
* This action checks to see if a number of Bool Variables has been assigned a certain value,
* and performs something accordingly if all checks are true.
*
* -> It only works with global variables
* -> It does not support action list parameters
* -> It only works with booleans
*
*/

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 ActionMultiBoolCheck ()
{
this.isDisplayed = true;
category = ActionCategory.Variable;
title = "MultiBoolCheck";
description = "Queries the value of one or more Global Bool Variables declared in the Variables Manager.";

for (int Count=0; Count < NumberOfBools; Count++) {
multiBool.Add(new MultiBool());
}

}


override public ActionEnd End (List<AC.Action> actions)
{
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 GenerateStopActionEnd ();
}

GVar var = GlobalVariables.GetVariable (_multiBool.variableID);

if (var != null) {
var.Download ();
// ACDebug.LogWarning ("The 'Variable: MultiBoolCheck' compares " + var.label + " which is "+ var.val+" with "+_multiBool.boolValue);
if (var.val == (int)_multiBool.boolValue) {
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 GenerateStopActionEnd ();
}
Count++;
}

if (Hits == NumberOfBools) {
// ACDebug.LogWarning ("The 'Variable: MultiBoolCheck' Action gives a Hits count of " + Hits+"/"+NumberOfBools);
return ProcessResult (true, actions);
} else {
// ACDebug.LogWarning ("The 'Variable: MultiBoolCheck' Action gives a Hits count of " + Hits+"/"+NumberOfBools);
return ProcessResult (false, actions);
// return GenerateStopActionEnd ();
}

}


#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)
{
// Wasn't found (variable was deleted?), so revert to zero
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;
}

}

}

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.