i’ve written something like 800 lines of code and now that i’ve finished i have a doubt about nested if statements. The code doesn’t seem to work but this could be because some referenced textures are still missing (i still have to draw them). My question is: is this “grammar” correct?
var gamemode : int;
var condition1 : boolean;
var condition2 : boolean;
var value1 : int;
function Update () {
if (gamemode == 1) {
if (condition1 == true) {
if (condition2 == true) {
value1 = 5;
}
}
}
}
It’s ok - although comparing a boolean variable to true is useless: this comparison always returns the variable value, thus you could just write
if (condition1){
Actually, boolean variables were created to be tested directly, like above. Comparing booleans isn’t wrong, however, and may even be advisable when you want to do something when the variable is false:
if (someBool == false){
results the same as
if (!someBool){
but looks more clear and easy to understand - not to mention that the negation operator (!) is thin enough to be ignored at first sight, making us misunderstand the code.
Your grammar for a small statement is fine. However, depending on the circumstances, a long piece of code with a long number of if statements might be made simpler through a loop (like a for… loop).