Knowledge Base - Making Decisions
Making Decisions
The real power of ACS becomes evident in its decision making capability. Until the advent of ZDoom, the only real decision making in Doom was related to keys. If the player had a key, a door would open or a device would activate. Now, a range of events can be executed based on a multitude of criteria.
IF
-ELSE
The IF-ELSE statement is the decision making verb in ACS. It is a Boolean decision statement and activates when the statement it evaluates is TRUE. TRUE is defined as any non-zero value. 1 is TRUE as is, 4, 10 and 1+1. FALSE is defined as zero. The format is:
if (evaluation)
{
commands;
}
else
{
commands;
}
The commands under the keyword IF will be executed if evaluation returns TRUE (or non-zero). If the evaluation is FALSE (zero), then the commands under the ELSE keyword are executed. Note that the ELSE statement is optional. Remember multiple commands must be enclosed in { and }.
For example, if A == 1 then: 'if (A)
' will return true and the statements following the if will be executed. If A == 0 then: 'if (A)
' will return false and the statements following the if
are ignored.
However, A is usually a statement not single variable. The IF statement uses logical operators to evaluate the statements enclosed in ( ). Logical operators evaluate an expression then return true or false. The logical operators are: !, &&, ||, ==, !=, >, <, >=, <=.
!
This is the NOT operator. This is the most difficult operator to understand because it operates in a backward fashion. That is, it inverts the truth of a statement. For example, an event must be triggered when a counter reaches zero. Remember, any non zero value is treated as true and an if statement only executes its commands if the expression evaluates to true. If A == 1, then !A will return false because the truth has been inverted from true (A == 1), to false. When A is zero, then !A will return true because the truth has been inverted from false (A == 1 is false) to true.
script 1 (int switch) { // If the player hasn't thrown the switch yet, // do nothing. if (!switch) print (s:"You need to activate a switch first"); // Execute these commands if the switch is thrown. else commands...; }
&&
This is the AND operator. This operator is used to test the "trueness" of all variables, in a set of variables. All of the variables tested must be true in order for the whole statement to be true. If any one variable is false, the whole statement evaluates to false.
script 1 (int sw1, sw2, sw3) { // If all three switches have been thrown, // then activate the radiation leak. if (sw1 && sw2 && sw3) Sector_SetDamage (1, 50, MOD_LAVA); // Turn off the radiation leak. else Sector_SetDamage (1, 0, MOD_WATER); }
||
This is the OR operator. This operator is used to test the "trueness" of any variable in a set of variables. If any variable is true, then the whole statement is true. If all the variables are false, then the statement returns false.
script 1 (int sw1, sw2) { // If either switch is thrown, turn on the light. if (sw1 || sw2) { Light_RaiseByValue (1, 180); print (s:"Ah, you can see now"); } }
==
This is the equality operator. This will return true if two variables or expressions are equal. (This is used instead of = so as not to be confused with the assignment operator '=').
script 1 (void) { if (ThingCount (0, 1) == 0) print (s:"All those nasty zombies are dead"); }
!=
This is the NOT EQUAL operator. It is, obviously, the opposite of the equality operator and will return true if two variables or expressions are not equal.
script 1 (void) { if (ThingCount (0, 1) != 0) print (s:"You must kill all the zombies"); }
>
This is the greater than operator and as the name implies, will return true if the variable or expression on the left side is greater than the variable or expression on the right side.
script 1 (void) { // Spawn a stealthvile after 10 seconds, because I am in // bad mood. :) if (Timer() > 350) Thing_SpawnNoFog (1, T_STEALTHVILE, 0); }
<
This is the less than operator and as the name implies, will return true if the variable or expression on the left side is less than the variable or expression on the right side.
script 1 (void) { // Let the player know I'm in a bad mood. if (Timer() < 350) print (s:"I'm in a bad mood, so watch it"); }
>=
This is the greater than or equal to operator. As the name implies, this operator will return true if the variable or expression on the left side is greater than or equal to the variable or expression on the right side.
script 1 (void) { // Spawn a stealthvile if 10 seconds have elapsed. if (Timer() >= 350) Thing_SpawnNoFog (1, T_STEALTHVILE, 0); }
<=
This is the less than or equal to operator As the name implies, this operator will return true if the variable or expression on the left side is less than or equal to the variable or expression on the right side.
script 1 (void) { // Let the player know something bad is going to happen. if (Timer() < 350) print (s:"In ten seconds, your day will be ruined"); }
Nested IFs
IF statements can also be nested as in the following example.
script 1 (int sw) { if (sw == 1) { if (ThingCount (0, 1) == 0) print (s:"You kill all the zombies"); else print (s:"The door is now open"); } }
It will take some time to get comfortable with how the logical operators work, but once this skill is learned, it will become a powerful scripting tool. The thing to remember is that if the IF part is true, the statements following the IF will execute. If the IF part is false, then the commands following the ELSE will be executed, if an else is present of course.