×
Меню
Индекс

MSFD Script with style for safer scripting

 
This is bound to be a bit controversial as it is as much about personal style as it is about fact. And it's bound to sound snobbish . Nevertheless, I think this might be of some use for the newcomer, so here are a few comments about my personal views on good style and safer scripting:
•     Use annotations. In a really short script they might seem useless, but even there you might want to state which mod/quest/item they belong to or what their general purpose is, etc. For long scripts this becomes indispensable, for yourself, if you stop working for a few days as well as for others that might want to learn from your script. Explain your variables, put headers on the main part of your script, comment on important lines of code, etc.
•     Use state variables with style. Basically, due to the "executed once a frame" nature of the scripts this is your principal way of structuring your script for sequential events. There are several things to this.
A) Limit yourself to the minimal amount you need for the script.
B) use elseifs to chain different states of one state variable together, not separate if-blocks – this should be the main structural element of your script (it's not always possible nor necessary, but if it is, its going to save you a lot of trouble).
C) Check the elseifs are arranged from lowest to highest and in a logical order of events – will help you to keep everything organized, and thus avoids bugs. Jumping around wildly with state variables is the equivalent to careless use of GOTO in good old BASIC.
D) Use them extensively, do small steps. I can't tell you how many times bugged scripts started working simply because I moved some functions to a separate "state-block". Sometimes this doesn’t seem to be logical at all – but if you can safely enter another step, do it.
•     Decide for one style. TES Script is relatively forgiving regarding syntax. You can write functions small letters or capitalized as in this document, or all caps. You can use if ( SomeFunction == 1 ) of if (somefunction). But whatever you choose, try to be consistent in your usage.
•     Use verbose variable names. A name that reflects the function of this variable makes a script much more readable. If you use global variables give them a unique name, e.g. put your initials in front or whatever – just try to minimize the chance that another mod will come up with the same name for a global – because that would screw things up royally.
•     Keep track of your Return function uses. The Return function is inherently dangerous – remember that it will stop anything in that script below that line from being executed. Use it, but use it sparingly. If you have the feeling you have to use it a lot in one script, you should probably introduce a state variable instead.