- Home /
If statement question
Hey,
Is it possible to simplify :
if ((Input.GetButtonDown("Attack") || Input.GetButtonDown("Escape") || Input.GetButtonDown("Start")) || Input.GetButtonDown("Select"))
Isn't there a way to just have the Input.GetButtonDown once, something like :
if (Input.GetButtonDown("Attack" || "Escape" || "Start" || "Select"))
Thanks.
Answer by Kishotta · Apr 22, 2017 at 07:47 PM
This is possible with a few variables. It doesn't get rid of the Input calls, but it does simplify the branch condition.
bool Attack = Input.GetButtonDown ("Attack");
bool Escape= Input.GetButtonDown ("Escape");
bool Start= Input.GetButtonDown ("Start");
bool Select= Input.GetButtonDown ("Select");
if (Attack || Escape || Start || Select) {
// ...
}
Answer by kritoa · Apr 23, 2017 at 07:29 AM
No. There are some specific things (like layers) where you can bitwise AND and OR things together like that but not for something like this.
Answer by Guichaoua · Apr 23, 2017 at 07:36 AM
Try this:
using System.Linq
public static bool GetAllButtonsDown(params string[] buttons)
{
return buttons.All( b => Input.GetButtonDown (b));
}
public static bool GetOneButtonsDown (params string[] buttons)
{
var check = (b) => Input.GetButtonDown (b);
foreach (var but in buttons)
if (check (but)){
return true;
}
return false;
}
While this is possible it's quite inefficient. Why do you actually wrap GetButtonDown in a lambda expression which you recreate everytime you call that method? This approach creates quite a bit of garbage each time you call it. First the "params array", second the unnecessary lambda and finally the foreach IEnumerable.
$$anonymous$$ost methods that provide a params array have multiple specific overloads for 1,2,3 and 4 parameters and the params array is only used if more parameters are needed. In case of strings you could simply do:
public static bool GetAnyButtonDown(string b1, string b2 = null, string b3 = null, string b4=null)
{
if (b1 != null && Input.GetButtonDown(b1)) return true;
if (b2 != null && Input.GetButtonDown(b2)) return true;
if (b3 != null && Input.GetButtonDown(b3)) return true;
if (b4 != null && Input.GetButtonDown(b4)) return true;
return false;
}
public static bool GetAnyButtonDown(params string[] more)
{
if (more == null)
return false;
for (int i = 0; i < more.Length; i++)
if (Input.GetButtonDown(more[i]))
return true;
return false;
}
Ins$$anonymous$$d of default parameters for b2, b3 and b4 you could create seperate overloads to $$anonymous$$imize the overhead, but it would be a micro optimisation. You should avoid using this with more than 4 parameters as it requires the compiler to create an array each time the method is called. If you frequently need more than 4 you may want to create more overloads.
However if you need this only one or two times, there's no point in creating such a method in the first place. The fastest and most readable implementation is:
if (Input.GetButtonDown("Button1") ||
Input.GetButtonDown("Button2") ||
Input.GetButtonDown("Button3") ||
Input.GetButtonDown("Button4")
)
{
// do something
}
If you use a proper IDE (like Visual Studio) it will even format it for you. Visual Studio can be heavily customized what style it uses for auto formatting.
Where would we be without your expertise?! You're awesome, just in case you hadn't been told lately.