18,700
edits
(→Logical: bare bool references don't work right in Windows Oni) |
(→if: don't encourage reader to use bools now that we know how buggy they are in Windows) |
||
Line 152: | Line 152: | ||
The result of the operation within the parentheses will always boil down to either "true" or "false" (a "yes" or a "no"), even if you would think that the result would be a number. For instance: | The result of the operation within the parentheses will always boil down to either "true" or "false" (a "yes" or a "no"), even if you would think that the result would be a number. For instance: | ||
if (8 - 8) # evaluates | if (8 - 8) # evaluates as false because 8 minus 8 is zero | ||
if (8 - 7) # evaluates | if (8 - 7) # evaluates as true because 8 minus 7 is non-zero | ||
Braces are optional when you only have one line of code under the "if" (this is also true for "else" and "else if", discussed below): | Braces are optional when you only have one line of code under the "if" (this is also true for "else" and "else if", discussed below): | ||
Line 159: | Line 159: | ||
if (a > 0) | if (a > 0) | ||
spawn_enemy_at_flag(a); | spawn_enemy_at_flag(a); | ||
spawn_considered = | spawn_considered = 1; | ||
Here, "spawn_considered" will be set to true regardless of whether the "if" condition was true and spawn_enemy_at_flag() was run. (The indentation has no effect on how the code runs; it's simply to guide the reader.) Though it's nice to save the vertical space by omitting the braces, the danger is in writing something like this: | Here, "spawn_considered" will be set to true regardless of whether the "if" condition was true and spawn_enemy_at_flag() was run. (The indentation has no effect on how the code runs; it's simply to guide the reader.) Though it's nice to save the vertical space by omitting the braces, the danger is in writing something like this: | ||
Line 165: | Line 165: | ||
if (a > 0) | if (a > 0) | ||
spawn_enemy_at_flag(a); | spawn_enemy_at_flag(a); | ||
spawn_considered = | spawn_considered = 1; | ||
That indentation on "spawn_considered" is misleading; when glancing at this code, you might expect that "spawn_considered" only is changed if "a" is greater than zero. It could be the script's writer really did want that line to be subject to the "if" statement, but he forgot to add braces. Always using braces around an "if" statement's body, even when it's one line long, will prevent that mistake. | That indentation on "spawn_considered" is misleading; when glancing at this code, you might expect that "spawn_considered" only is changed if "a" is greater than zero. It could be the script's writer really did want that line to be subject to the "if" statement, but he forgot to add braces. Always using braces around an "if" statement's body, even when it's one line long, will prevent that mistake. | ||
Line 175: | Line 175: | ||
var int one = 1; | var int one = 1; | ||
var int two = 2; | var int two = 2; | ||
var | var int one_equals_two = 0; | ||
if (one eq two) # this condition will evaluate to false, but... | if (one eq two) # this condition will evaluate to false, but... | ||
one_equals_two = | one_equals_two = 1; # ...this will still run | ||
if (one_equals_two eq | if (one_equals_two eq 1) | ||
dprint("Uh-oh."); | dprint("Uh-oh."); | ||
else | else | ||
Line 188: | Line 188: | ||
Bungie West was aware of this bug, and avoided it by never setting variables under "if" statements. However, you can safely do so as long as you use a global variable. For some reason, assigning a global variable under an "if" will not fire out of scope: | Bungie West was aware of this bug, and avoided it by never setting variables under "if" statements. However, you can safely do so as long as you use a global variable. For some reason, assigning a global variable under an "if" will not fire out of scope: | ||
var | var int one_equals_two = 0; | ||
func void fixed_if(void) | func void fixed_if(void) | ||
Line 196: | Line 196: | ||
if (one eq two) | if (one eq two) | ||
one_equals_two = | one_equals_two = 1; | ||
if (one_equals_two eq | if (one_equals_two eq 1) | ||
dprint("Uh-oh."); | dprint("Uh-oh."); | ||
else | else | ||
Line 213: | Line 213: | ||
Another unused feature of BSL is that you can express the opposite of some condition by using the "!" operator (pronounced "not"). The following two statements have the same meaning: | Another unused feature of BSL is that you can express the opposite of some condition by using the "!" operator (pronounced "not"). The following two statements have the same meaning: | ||
if (!(one_equals_two eq | if (!(one_equals_two eq 1)) | ||
if (one_equals_two eq | if (one_equals_two eq 0) | ||
====else==== | ====else==== |