- Home /
Getting repeated inputs when I want one at a time.
I want to press "e" to select the attack menu, then press "e" again to use an attack. But, if I press "e" both the menu changes AND the attack is confirmed.
if (currentMenu == "combatMenu"){
if (Input.GetButtonDown("e")){
currentMenu = "attackMenu";
}
}
if (currentMenu == "attackMenu"){
if (Input.GetButtonDown("e")){
print "Attack Confirmed";
}
}
How do I fix this?
Answer by fafase · May 30, 2014 at 06:55 AM
You need to add a condition to the second.
if the first one is pressed you put the condition to true and then the second needs that condition to run.
Then you could also set the condition back to false after a certain time.
But this is what you are doing but you are doing it the wrong way:
if (currentMenu == "combatMenu"){
if (Input.GetButtonDown("e")){
currentMenu = "attackMenu";
}
}
else if (currentMenu == "attackMenu"){
if (Input.GetButtonDown("e")){
print "Attack Confirmed";
}
}
The else if makes the second part being ignored if the first one is run. So on the first round, it will go inside the first part and turning your variable. The second part is skipped from the first round. Second round, the first if is false so it goes to check for the second and it works.
Adding the 'else' fixed the problem, but I don't understand why it's needed. Shouldn't the second part just return false the first round and skip the input?
Never underestimate how stupid a computer can be. It does things as he sees them.
if (current$$anonymous$$enu == "combat$$anonymous$$enu"){ // This is true, I get in
if (Input.GetButtonDown("e")){ // This is true, I get in
current$$anonymous$$enu = "attack$$anonymous$$enu"; // I change this
}
}// I am out of the statement and I continue
if (current$$anonymous$$enu == "attack$$anonymous$$enu"){ // This is true, since I just change it two lines ago, I get in
if (Input.GetButtonDown("e")){ // This is also true since it is true for the whole frame
print "Attack Confirmed"; // I do that even though you don't want it now
}
}
Extra advice for free, avoid using string for your state, use an enum. They are faster and less prone to error since you cannot pass whatever value but the one in the enum.
Thank you for the advice! I didn't realize the variable would be changed within the same frame, and that the input condition would remain true for the rest of the frame as well. This clears up a lot.
As for the enums I haven't learned about those yet. This is all experimental code while I $$anonymous$$ch myself scripting. Next thing I do will be to learn all about enums.
Answer by Andres-Fernandez · May 30, 2014 at 07:03 AM
It's a problem with your logic. Just debug the code with monodevelop, you'll see that in the same frame your currentMenu changes from "combatMenu" to AttackMenu", and as Input.GetButtonDown is true both times you call it (actually it will be true all the times you call it in the same frame) both if statements will be true. Try it this way:
if (Input.GetButtonDown("e")) {
if (currentMenu == "combatMenu") {
currentMenu = "attackMenu";
} else if (currentMenu == "attackMenu"){
print "Attack Confirmed";
}
}
So, is it good practice to always use else statements when I have a list of conditions? For example, here's the same code but in more context:
if (current$$anonymous$$enu == "combat$$anonymous$$enu"){
if (e) current$$anonymous$$enu = "attack$$anonymous$$enu";
if (s) current$$anonymous$$enu = "item$$anonymous$$enu";
if (d) current$$anonymous$$enu = "move$$anonymous$$enu";
if (f) current$$anonymous$$enu = "magic$$anonymous$$enu";
if (mouse1) current$$anonymous$$enu = "select$$anonymous$$enu";
}
Should it ins$$anonymous$$d look like this?
if (current$$anonymous$$enu == "combat$$anonymous$$enu"){
if (e) current$$anonymous$$enu = "attack$$anonymous$$enu";
else if (s) current$$anonymous$$enu = "item$$anonymous$$enu";
else if (d) current$$anonymous$$enu = "move$$anonymous$$enu";
else if (f) current$$anonymous$$enu = "magic$$anonymous$$enu";
else if (mouse1) current$$anonymous$$enu = "select$$anonymous$$enu";
}
Only if you are meant to have one condition at a time.
Actually, in your case I would have used a switch statement which avoid this kind of bug. But you said to be in a learning state so you may not have seen them yet.
So switch and enumeration is what I would go for.