I am working with boolean arrays and I have been having trouble getting all the booleans in an array to be false when one of them is set to true.
I have been trying this code but it does not work at all.
private static bool SetUP,SetDown,SetLeft,SetRight;
private static bool[] boolarray = {SetUP,SetDown,SetLeft,SetRight};
bool SetBoolArrays(bool[] array, int lowVal, int highVal, bool State)
{
for (int i = lowVal; i < highVal; i++) { array *= State; } return State;*
}
void OnGUI() { // make everything from position 1 to array.Lenth false
//method 1 if(boolarray[0])SetBoolArrays(boolarray,1,boolarray.Length,false); // does not work
//method 2 if (boolarray[0]) { // will be set to true but will not be set to false when boolarray[1] is true System.Array.Clear(boolarray, 0, boolarray.Length); SetUp = true; }
if (boolarray[1]) { // will be set to true but will not be set to false when boolarray[0] is true System.Array.Clear(boolarray, 0, boolarray.Length); SetDown = true; }
I would suggest taking a look at EnumFlags. It’s pretty much what you are trying to achieve but it has the advantage of mixing values and being much smaller on memory(Not that a bool is huge). There are some advanced stuff with the code below but it should be pretty simple to understand when you read over it.
using UnityEngine;
using System.Collections;
public class FlagTest : MonoBehaviour
{
/// <summary>
/// The types of clothing our person can wear. It uses bitwise to
/// give it the ability to have multiple states. If everything
/// were off it would be 000000000 and if everythign were on it would be 111111111. What
/// we do is turn that ones we want on to 1. So say we wanted a dress with sandles it would
/// be 0100001000.
/// </summary>
[System.Flags]
private enum ClothingTypes : int
{
Hat = 1 << 0,
Sandles = 1 << 1,
Toga = 1 << 2,
PaperBag = 1 << 3,
Shorts = 1 << 4,
PantSuit = 1 << 5,
Dress = 1 << 6,
Sunglasses = 1 << 7,
Earings = 1 << 8,
Socks = 1 << 9 // Bitshifting :)
}
/// <summary>
/// The clothing they are wearing now.
/// </summary>
private ClothingTypes currentlyWearing;
public void Awake()
{
//Are we wearing a hat?
Debug.Log( "Wearing Hat: " + HasFlag( ClothingTypes.Hat ) );
//Well now we are
SetFlag( ClothingTypes.Hat, true );
//Lets just make sure we are wearing our hat
Debug.Log( "Wearing Hat: " + HasFlag( ClothingTypes.Hat ) );
//Now take the hat off
SetFlag( ClothingTypes.Hat, false );
//This just confirms that in fact we don't have a hat on anymore.
Debug.Log( "Wearing Hat: " + HasFlag( ClothingTypes.Hat ) );
//Now put on that dress
SetFlag( ClothingTypes.Dress, true );
//Are we wearing a dress with our sandles?
Debug.Log("Wearing PantSuit & Sandles: " + HasFlag(ClothingTypes.Sandles | ClothingTypes.PantSuit));
//Turns out we only had a dress on. Lets put on dem sandles
SetFlag( ClothingTypes.Sandles, true );
//We can now confirm that we are wearing a dress with our sandles
Debug.Log( "Wearing PantSuit & Sandles: " + HasFlag( ClothingTypes.Sandles | ClothingTypes.PantSuit ) );
//????
//Profit!
}
/// <summary>
/// Is this flag currently on?
/// </summary>
/// <param name="flags">What is the name of the flag?</param>
/// <returns>If it's on our not</returns>
private bool HasFlag( ClothingTypes flags )
{
return (currentlyWearing & flags) != 0;
}
/// <summary>
/// Set the value of the flag. It can be anything you want if all you want is true of false.
/// </summary>
/// <param name="flags">The flag you want to set</param>
/// <param name="val">If you want it true of false</param>
private void SetFlag( ClothingTypes flags, bool val )
{
if(val == true)
{
currentlyWearing |= flags;
}
else
{
currentlyWearing &= ~flags;
}
}
}
You can then turn everything off by going
currentlyWearing = 0
Edit:
As Bonfire Boy said I did not add the answer to your question. To turn all other bools off you could do
currentlyWearing = ClothingTypes.Hat;
// Or event all off but two
currentlyWearing = ClothingTypes.Hat | ClothingTypes.Toga