- Home /
The question is answered, right answer was accepted
How do I make ALL other booleans in a bool array false when one is true ?
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[i] = 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;
}
}
nothing happens . absolutely nothing . I have just tried System.Array.Clear(boolarray, 0, boolarray.Length);
and still , nothing happens
That code sets all the values in your array (except the first one) to false, but they've all been initialised to false anyway (because they're initialised to the values of 4 bools that have been declared without initialisation - they take the default value). So it doesn't look like there's any code there to change anything.
The point is, how do know that nothing happens? Have you tried logging the array values before and after the call to SetBoolArrays()?
It's not really clear to me what you're trying to achieve. The question is ambiguous at best (is it "when ANY one of them is true" or "when a particular one is true"). The code suggests maybe you want to do it when the first one is true, in which case you could change your OnGUI to
if (boolarray[0]) SetBoolArrays(boolarray,1,boolarray.Length,false);
Also there's a syntax error in the first line (comma after bool
) so I wouldn't expect this code to even compile!
yeahn I have run a Debug.Log. I just wrote this code here for an example, I will fix the syntax error . I already tried that method , it wont work.
nothing will be set to false in the array but if you Debug.Log(array[i]) in the SetBoolArray function in the for loop , it will print false for each but nothing will actually be set to false .
as a metter of fact anything can be set to true in code , but nothing is set to false .
i just tried
if (boolarray[0])
{
System.Array.Clear(boolarray, 0, boolarray.Length);
SetUp = true;
}
if (boolarray[1])
{
System.Array.Clear(boolarray, 0, boolarray.Length);
SetDown = true;
}
both will be made true , nothing will be made false. which i don't understand
@meat5000 That makes sense if one only ever wants one of them to be true. But (and B$$anonymous$$ayne's answer provides a handy example here) you might only want to clear all the other clothing flags when "WetSuit" is true, not for example when "Hat" is true :) But this just goes to support what your saying, that the best way to do whatever-it-is is going to depend on the application.
Answer by BMayne · Feb 09, 2015 at 12:52 AM
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
I think you're right that this is probably the kind of thing they're after, but it doesn't yet address the question of setting all the other flags to false if one of them is true. I guess that would be something like
if (HasFlag(ClothingTypes.Hat))
{
currentlyWearing = ClothingTypes.Hat;
}
hey, thanks for all your help. thanks to Bonfire Boy I think I can fix the issue now.
Answer by Bonfire-Boy · Feb 09, 2015 at 01:25 AM
Still not quite clear to me, but one thing I think you're not understanding is that when you do this
private static bool[] boolarray = {SetUP,SetDown,SetLeft,SetRight};
it's creating an array containing 4 new bools which are initialised to the same values as those 4 variables. They're not references to them.
So changing the values of SetDown, SetUp, etc has no impact on your array.
I think BMayne's answer is pointing you in a good direction.
thank you both, I have gotten this fixed. @Bonfire Boy as you had made mention to , my booleans in boolarray were not referencing to the initially declared booleans.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to make the Boolean equal to true for only one object 1 Answer
bool[string] = true; array possible? 2 Answers
Array Of Bools 4 Answers