- Home /
The question is answered, right answer was accepted
How to check the state of booleans in an bool array, reading from index "a" to "b"?
Hi I have a boolean array private bool[] boolarray = new bool[7];
lets say i want to check the state of the booleans at positions 3 to 7.
so if positions 3 to 7 == true ....
I am hoping to be able to check the state of the boolean in that way or along those lines
how is this done , can it even be done?, if not what is the best method to achieve the desired result.
Answer by Mmmpies · Feb 08, 2015 at 02:42 PM
using UnityEngine;
using System.Collections;
public class CheckArr : MonoBehaviour {
private bool[] boolarray = new bool[7];
private bool returnedBool;
// Use this for initialization
void Start () {
boolarray[0] = true;
boolarray[1] = true;
boolarray[2] = false;
boolarray[3] = true;
boolarray[4] = true;
boolarray[5] = true;
boolarray[6] = true;
returnedBool = CheckBoolArrays(1, 4);
Debug.Log (returnedBool);
returnedBool = CheckBoolArrays(4, 6);
Debug.Log (returnedBool);
}
bool CheckBoolArrays(int lowVal, int highVal)
{
for(int i = lowVal; i <= highVal; i++)
{
if(boolarray[i] == false)
return false;
}
return true;
}
}
This should work, just be careful with passing only up to 6 for an array of 7.
okay great! , this is good . thanks for the help and the code .
And thumbs up @ruchmair for not only ticking an answer but thumbs up on the other 2 solutions offered :¬)
yea, you would have to write another function for that, it wouldn't need a return type but would need another parameter for the bool so you would call something like SetBoolArray(array, a, b, boolName);
and just use the same for loop but ins$$anonymous$$d of the If & returns just put in boolarray[I] = boolName;
bool CheckBoolArrays(string myArray, int lowVal, int highVal)
{
for(int i = lowVal; i <= highVal; i++)
{
switch(myArray)
{
case "boolarray" :
if(boolarray[i] == false)
{
return false;
}
break;
case "otherarray" :
if(otherarray[i] == false)
{
return false;
}
break;
}
}
return true;
}
Bit of a cheesy way to do it but if you're having issues it'll get you through. Incidentally are these all being called from the same script, or will the bools be from all different scripts?
If from different then you'll need to reference the script to get to the bool. Call the above with:
returnedBool = CheckBoolArrays("boolarray", 1, 4);
Debug.Log (returnedBool);
returnedBool = CheckBoolArrays("boolarray", 4, 6);
Debug.Log (returnedBool);
ah sorry if I was unclear, it's crashing because you are endlessly recurring the same function.
But to fix it just change the return
to:
return array;
and when you call it use something like:
originalArray = SetBoolArrays(originalArray, 3, 7, false);
that should return the array that is manipulated by the method to the original array.
Answer by Local-Minimum-2 · Feb 08, 2015 at 02:59 PM
This would dutifully print out both debug messages, which I think is the logic you wanted.
using system.linq
bool[] boolarray = new bool[7] {true, false, true, true, true, true, true};
if (boolarray.Skip(2).Take(5).All(v => v))
Debug.Log ("All true");
boolarray = new bool[7] {true, false, true, true, false, true, true};
if (!boolarray.Skip(2).Take(5).All(v => v))
Debug.Log ("All not true");
also great, this is even more concise . thanks for the help and the code
Answer by AlwaysSunny · Feb 08, 2015 at 02:44 PM
If you need the snippet in more than one class, or ever want to do it again in another project, it's a great idea to start keeping a file of handy static and extension methods.
public static bool CheckBoolRange(bool[] source, int a, int b) {
// here you might have checks to prevent out-of-range or null exceptions
for (int i=a; i<=b; i++) if(!source[i]) return false;
return true;
}
If that's more trouble than you want, the loop is a good enough answer:
bool result = true;
for (int i=a; i<=b; i++) if(!source[i]) result = false;
// now "result" answers your if-range-true query
Yea I was thinking extension too. In order to do that make sure it is in a separate class that is public static
, and that does not inherit from monodevelop
and use this bool[] source
in the method parameters.
Sample:
public static class Extensions {
public static Vector2 ToVector2(this Vector3 v3) {
return new Vector2(v3.x, v3.y);
}
}
I think there is also a Unity tutorial on it. And definitely check for index out of range.
sure , I will be using that snippet in a separate class . things will be more efficient that way . thanks for all the help.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Loop through multidimensional array in C#(Unity) 1 Answer
Unity event calling function gets nullreferencexception on bool 0 Answers
Change sprite of image 1 Answer