Difference between revisions of "If"
Jump to navigation
Jump to search
→Oblivion evaluates entire If statement
imported>Qazaaq (→Oblivion evaluates entire If statement: more code hardly ever causes lag in oblivion, removing statement) |
imported>Thalassicus |
||
Line 80: | Line 80: | ||
=== Oblivion evaluates entire If statement === | === Oblivion evaluates entire If statement === | ||
Oblivion evaluates all the conditions for an | Oblivion evaluates all the conditions for an If statement. For example, when you combine expressions with "&&", if the first expression is false, later expressions will still be evaluated, even though they are irrelevant (false && anything = always false, true || anything = always true). This results in more code being processed than necessary. | ||
This can also result in unexpected errors, such as below: | This can also result in unexpected errors, such as below: | ||
<pre>If (ReferenceVariable != 0) && (ReferenceVariable.Getav Health < 30) | |||
(code...) | |||
endif</pre> | |||
This crashes Oblivion if the reference variable is undefined ("0"), because the second part is still evaluated. | This crashes Oblivion if the reference variable is undefined ("0"), because the second part is still evaluated. Instead, use one If within another ("nested If statements"): | ||
<pre>If ReferenceVariable != 0 | |||
If ReferenceVariable.Getav Health < 30 | If ReferenceVariable.Getav Health < 30 | ||
(code...) | |||
endif | |||
endif</pre> | |||
'''Since boolean operators (&& ||) are implemented poorly in Oblivion, | '''Since boolean operators (&& ||) are implemented poorly in Oblivion, avoid them whenever possible.''' | ||
In addition, the script engine looks over everything inside an If block, even when the condition was false, until the script engine finds an exit point. An exit point is an accessible RETURN call, or the end of the script. | |||
This: | |||
<pre>;; unoptimized | |||
begin GameMode | |||
If (somecondition != 0) | |||
(...some inefficient/complex algorithm...) | |||
endif | |||
end</pre> | |||
Is more expensive than this: | |||
<pre>;; optimized | |||
begin GameMode | |||
If (somecondition == 0) ;; logical negation | |||
RETURN | |||
endif | |||
(...some inefficient/complex algorithm...) | |||
end</pre> | |||
In short, best practice is to call RETURN early and often, when possible. For large scripts with complex routines this subtle difference can yield dramatic performance gains. | |||
== Comparisons and Expressions == | == Comparisons and Expressions == |