[dismiss]
This wiki is a copy of the original Oblivion CS wiki created and maintained by the UESP.net. See CSwiki:Copy Notice for more info.
Difference between revisions of "If"
Jump to navigation
Jump to search
Added section on the use of Elseif and Else
imported>Clearance m (corrected link) |
imported>Saebel (Added section on the use of Elseif and Else) |
||
Line 184: | Line 184: | ||
set bValue to ( bValue == 0 ) | set bValue to ( bValue == 0 ) | ||
If bValue is intially 1 (True), then it will be reset to 0 (False). Or if initially 0 (False), it will be reset to 1 (True). | If bValue is intially 1 (True), then it will be reset to 0 (False). Or if initially 0 (False), it will be reset to 1 (True). | ||
== Use of Else and Elseif == | |||
Although Elseif and Else are not required for an If/Endif block, they can be very useful. Here are a few basics to remember: | |||
=== Else === | |||
- There can only be one Else in an If/Endif block and it must follow after any and all other Elseifs | |||
- An Else should only be used when, in the absence of all previous If/Elseif criteria, something should definitely happen. | |||
=== Elseif === | |||
- Elseifs are useful if you want to make sure that a certain thing only happens once, even though there are potentially multiple sets of criteria that could lead to the same result. | |||
For example, with the following code: | |||
if a && b | |||
call FunctionScriptA | |||
endif | |||
if c && d | |||
call FunctionScriptA | |||
endif | |||
If by chance a && b is true, as well as c && d, then FunctionScriptA will get called twice in a row. If you only want it to be called once, then you would use elseif as follows: | |||
if a && b | |||
call FunctionScriptA | |||
elseif c && d | |||
call FunctionScriptA | |||
endif | |||
- Elseifs are also very helpful when there are a variety of different events that can happen under different conditions, but only one of the events should occur: | |||
if aInt == 23 | |||
set myVariable to 1 | |||
elseif bRef == PlayerRef | |||
set myVariable to 2 | |||
elseif cString == "Lich" | |||
set myVariable to 3 | |||
endif | |||
- You can also potentially use Elseifs to break up a complex If statement that uses lots of logical ORs. Instead of: | |||
if (a && b) || (h && k) || (x && y) | |||
do stuff | |||
else | |||
do other stuff | |||
endif | |||
you could do: | |||
if a && b | |||
do stuff | |||
elseif h && k | |||
do same stuff as above | |||
elseif x && y | |||
do same stuff as above | |||
else | |||
do other stuff | |||
endif | |||
As mentioned earlier in this page, Oblivion will evaluate an entire If line, even when it technically would not need to. This technique avoids that issue by breaking the OR criteria into separate chunks. So even though there are more lines of code, performance-wise it may be more efficient, depending on what the criteria is. | |||
'''Tip:''' When deciding which order to do your Ifs/Elseifs, go in ascending order of simplicity/complexity. The first If statement should be very easy for the Oblivion engine to calculate. The next should be the second easiest, and so on. | |||
'''Disclaimer:''' You may want to ignore the priority of simplicity/complexity depending on the importance of the If/Elseif criteria. If there are two sets of criteria of equal value, then put the simpler to evaluate criteria first. If one set of criteria has to be checked first before processing the rest, then that becomes the priority, regardless of complexity. | |||
== Notes == | == Notes == |