×
Меню
Индекс

MSFD Operators / mathematical calculations

You can use the standard operators to do calculations in scripts:
 
Addition: +
Subtraction: -
Multiplication: *
Division: /
 
The syntax is as follows:
 
Set result_var to (var_a + var_b)
 
Instead of variables, literal values are also allowed. I assume that standard operator precedence applies ( * and / are calculated before +/- ). Since it has not been properly tested I usually always use parentheses to be on the safe side. You can use parentheses according to standard mathematical rules:
 
set ln to ( ln + ( k10 * math_ln10 ) + ( k2 * math_ln2 ) )
 
A warning: There are different opinions on the forums on the use of several operators in one line. Some people report a lot of problems with this, I myself have successfully used at least four operators and variables in a single line. There appears to be an issue with very long additions (e.g. adding up more than 20 variables in one line of code) that causes a mod to crash the game upon loading. If this happens, split the calculations to several lines.
 
Notes by DinkumThinkum:
Eleven variables in a single set statement will cause the following error when loading the game, followed by a crash to desktop:
"Need more room for zero pointers in Script::ReplaceGlobalsInData"
as soon as you click the button to acknowledge the error, CTD. (That script name wasn't any script of mine;  it's something internal to the game.)
 
Six variables in a Set statement work fine. Don't know the maximum number, but it's obviously at least six and definitely less than eleven.
 
There isn't much in the way of dedicated mathematical functions in TES script. There is the 'Random' function and Tribunal added the 'GetSquareRoot' function (see below). If you need more complex functions, I suggest downloading Soralis' Math Mod (available from Planet Elder Scrolls). It’s a collection of scripts that allow you to do complex calculations. Here is a short excerpt from the readme to give you an idea:
 
"This mod adds the ability to use various math functions within Morrowind's scripts.
Specifically, these are the scripts that are added:"
Name     Check/Done     Inputs     Outputs     Accuracy
MathScripts       N/A       N/A       N/A      N/A
MathConstants      N/A         N/A        N/A        N/A
SquareRoot         1       math_sqrt         math_result, math_imag         7
SineScript          2        math_angle        math_sin, math_cos, math_tan      7
ArcsineScript       3         math_arc            math_sin, math_cos        6-7
NaturalLog         4       math_log          math_result, math_imag        4-5
LogScript        5      math_log, math_base      math_result, math_imag       3-4
intPower           6      math_value, math_power      math_result         7
intRoot           7      math_value, math_root      math_result, math_imag        6-7
Modulus        8     math_value, math_mod      math_result      6-7
Antiln        9       math_log      math_result       4-5
Antilog       10       math_log, math_base        math_result         2-3
AbsoluteValue       11*       math_abs             math_abs          7
PowerScript        12      math_value, math_power      math_result, math_imag      2-3
 
Unfortunately many of these functions are rather slow, and not suited for real time calculations. For sine and cosine in particular, check out JDGBOLT's script in the tips and tricks section, which uses pre-calculated values for a very rapid calculation. You can also find more information on UESP Wiki.
MWSE (Morrowind Script Extender) also adds trig functions to Morrowind.