If you plan a mod with lots of scripts or long and involved scripts, you may want to give some thought to not wasting CPU power. There are a number of things you can do here:
If the script does not have to be executed every frame, put a little counter in there:
Begin My_super_long_script
Short framecounter
If ( framecounter < 10 )
set framecounter to ( framecounter + 1 )
Return
Endif
set framecounter to 0
[super long script goes here]
end
This little piece of code, that should be at the very top of your script, will allow your script (or rather the main, CPU power eating part of it) to be executed only every 10th frame. You could do the same thing with a timer, and execute the script only every 3 seconds or once every minute.
Execute script only if the player is suitably near. If you have scripted a fancy magic bouncing ball, or basically anything that is a visible effect, there is no reason to run the script if the player cannot see it. So put something like this on top of your script:
If ( GetDistance, player < 5000 )
Return
Endif
However, as getDistance isn’t an amazingly fast function, you wouldn’t want to put this check above a simple getScale, setScale script.
Shortcut scripts that are no longer needed. If you have local scripts that you may not need from a certain point onwards, e.g. because of Actor death or because the object was disabled, reduce their CPU need by putting something like the following at the top of the script:
if ( GetDisabled == 1 )
Return
Endif
If ( GetHealth <= 0 )
Return
Endif
Terminate global scripts. Remember, global scripts are running all the time until you stop them again with StopScript. You can do do-once global scripts by just putting a StopScript command at their end
Begin do_once_global_script
[your code here]
StopScript "do_once_global_script"
End
Try to use local scripts instead of global scripts. With local scripts you are sure that they only run when you are in the vicinity. Think hard before making a script global if it can be done with a local script instead.
Be careful with while-loops, GetDetected, GetLOS and other "slow" functions. Use methods as described above (e.g. a counter or a timer) to make sure they are not called too often.
Stop script while in menu mode. Always, unless you have specific reason not to, put the following at the top, to avoid the mouse lagging in the menu and other unwanted effects.
If ( MenuMode == 1 )
Return
Endif
In general, reducing the number of scripts running improves efficiency more than reducing the complexity of individual scripts.
So, for example, if you have two short globals running all the time, it is better to merge them into one global.
If you've got a few global scripts which only need to run based on some check, it makes sense to place the checks in one "parent" script, and startscript the others from there when they need to run (remembering to ensure to run them once per game session if they need to conserve variable values).