'How to check multiple bools in one
I need to create a manager that allows to add/remove bools and then have one condition to check if any are true.
I created a list and then used Contains(true) to check if any were true but then I realized that it doesn't re-check the bool methods (for example some of the bools will check if a certain button is being pressed).
Any ideas instead of creating a long list of if statements?
public static List<bool> BoolList = new List<bool>() { isleftdown() };
public static bool isleftdown(){
return Main.MouseButtons == MouseButtons.Left;
}
//Loop
if (BoolList.Contains(true)) {}
Solution 1:[1]
You can do this with a list of Func<bool>. Instead of passing in the result of the function, you pass in the function itself. For example:
public static List<Func<bool>> FuncList = new List<Func<bool>>
{
isleftdown, isRightDown, otherFunc
};
And to check the results with some simple Linq:
// Are all functions true:
var allTrue = FuncList.All(f => f());
// Are any functions true:
var anyTrue = FuncList.Any(f => f());
// Are all functions false:
var anyFalse = FuncList.All(f => !f());
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DavidG |
