"If any element of an array is true? "

I have an array list with various inputs of the like "Input.GetKeyDown("whatever")" and I would like to know how to recognize if any of those inputs are pressed in an "if" statement and to push it in a new array during a loop.

for example something like :

var arr = Array (Input.GetKeyDown("space"), Input.GetKeyDown("f"));
var arr2 = new Array ();
var desiredLength = 4;
var arr2.length = desiredLength;

 for (i = 0; i < desiredLength; i++){
        if (any of the arr[] is true) { // if any of the array inputs is pressed
            arr2.Push (arr[whatever index was just pressed]);
        }
    }

Or do I have to make an "if" for each button possibility of the array?

2 Answers

2

You would usually do this:

if ( arr *== true )*
 _arr2.Push(arr*)*_
_*```*_
_*<p>However, I've never tried it with an array of inputs.  Give it a shot and let me know how it goes!</p>*_
_*<hr>*_
_*<p>The complete code, from your answer:</p>*_
_*```*_
_*for (i = 0; i < desiredLength; i++)*_
_*{*_
 <em>_if ( arr *== true )*_</em>
 <em><em>_arr2.Push(arr*)*_</em></em>
<em><em>_*}*_</em></em>
<em><em>_*```*_</em></em>

So you mean I would have to make a nested loop where i is the number of places in arr2 while j would be related to what's inside the original array with the inputs?

Yeah I know and I tested with random strings inside before doing the array of inputs that would require a timer (quicker to test) and as expected it gives me an error message because the Index is greater than the list count. In that example if "i" reaches 3, it can't find anything in the arr since it doesn't go above arr[2] and thus can't push anything for arr2.Push(arr[3]) since arr[3] doesn't exist.

To clarify what I'm aiming at is basically for each turn, allow the player to press an input that is among several choices inside an array to fill a new array of X length (basically the new array will recreate/record the sequence of inputs the players did during those X loops).

the line if ( arr == true ) can be resumed to if(arr*)* you can also nest all Marowi code inside a if(Input.AnyKeyDown) and use return true to check if an input that's other than one on the array was pressed in case it's useful for you.

If you use a List instead of an Array, you can use:

`List.Any()`

This come from the Enumerable class, so there are even more data structures that support this feature.

Extension methods don't work in UnityJScript though. You can still call the static class for what it's worth.