Difference between revisions of "Minimizing your Script"
imported>Haama (Created) |
imported>XJDHDR m (Small change to eliminate confusion for new scripters.) |
||
(6 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
The following is only important when a script does a CPU intensive task that is known or has been tested to affect the frame rate. This is generally reduced to situations that involve naive searching, sorting, or constant iteration through many references. Code that must have inefficient algorithms can best take advantage of the techniques here. Note that almost all code produced with the vanilla editor will already run well, regardless of programming technique. | |||
== Gamemode Scripts == | == Gamemode Scripts == | ||
Avoid using gamemode scripts wherever possible. Use quest scripts if you can, or try to find ways to put as much of the script work into OnLoad, OnEquip, and ScriptEffectStart blocks as possible. | Avoid using gamemode scripts wherever possible. Use quest scripts if you can, or try to find ways to put as much of the script work into OnLoad, OnEquip, and ScriptEffectStart blocks as possible. | ||
Line 18: | Line 20: | ||
endif | endif | ||
end</pre> | end</pre> | ||
== An important note on If blocks vs early Returns == | |||
The Morrowind scripting community determined that the script engine would process all code inside of an If block (even if the condition was false) until the script engine could find an exit point. An exit point can be either an accessible RETURN call, or the end of the script. It appears that this is also true in Oblivion - see the OblivionScriptsOptimized thread linked below. | |||
This: | |||
<pre>;; unoptimized | |||
begin GameMode | |||
if (some condition) != 0 | |||
(some inefficient/complex algorithm) | |||
endif | |||
end</pre> | |||
Is considerably more expensive than this: | |||
<pre>;; optimized | |||
begin GameMode | |||
if (some condition) == 0 ;; logical negation | |||
RETURN | |||
endif | |||
(some inefficient/complex algorithm) | |||
end</pre> | |||
In short, best practices for Oblivion scripting are to call RETURN early and often. For large scripts this subtle coding difference can yield dramatic performance gains. | |||
== External thread of tips == | |||
[http://www.bethsoft.com/bgsforums/index.php?showtopic=753010 OblivionScriptsOptimized] | |||
[[Category:Code Optimization]] |
Latest revision as of 11:54, 23 January 2010
The following is only important when a script does a CPU intensive task that is known or has been tested to affect the frame rate. This is generally reduced to situations that involve naive searching, sorting, or constant iteration through many references. Code that must have inefficient algorithms can best take advantage of the techniques here. Note that almost all code produced with the vanilla editor will already run well, regardless of programming technique.
Gamemode Scripts[edit | edit source]
Avoid using gamemode scripts wherever possible. Use quest scripts if you can, or try to find ways to put as much of the script work into OnLoad, OnEquip, and ScriptEffectStart blocks as possible.
If you need to use a GameMode block, use an 'if' test or a flag so the code will only run when necessary. For instance, if you need to run an item script whenever the player hits a switch, place this on the switch:
scn YourSwitchScript short Working begin onActivate set Working to 1 end
and this on the item:
scn YourItemScript begin GameMode if YourSwitchScript.Working ... endif end
An important note on If blocks vs early Returns[edit | edit source]
The Morrowind scripting community determined that the script engine would process all code inside of an If block (even if the condition was false) until the script engine could find an exit point. An exit point can be either an accessible RETURN call, or the end of the script. It appears that this is also true in Oblivion - see the OblivionScriptsOptimized thread linked below.
This:
;; unoptimized begin GameMode if (some condition) != 0 (some inefficient/complex algorithm) endif end
Is considerably more expensive than this:
;; optimized begin GameMode if (some condition) == 0 ;; logical negation RETURN endif (some inefficient/complex algorithm) end
In short, best practices for Oblivion scripting are to call RETURN early and often. For large scripts this subtle coding difference can yield dramatic performance gains.