Difference between revisions of "Vector keyword"
m (moved Vector (dascript type) to Vector keyword) |
m (Adding links) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
− | The '''vector''' type represents a position or direction in three dimensions. It consists of three [[float]] values representing x, y and z which are accessed using the "[[ | + | The '''vector''' type represents a position or direction in three dimensions. It consists of three [[float]] values representing x, y and z which are accessed using the "[[dot operator]]". The default value is (0.0, 0.0, 0.0). |
== Constructor == | == Constructor == | ||
− | + | The [[Scripting terminology#constructor|constructor]] for a vector is the [[Vector]] function. | |
− | + | ||
− | + | ||
== Literals == | == Literals == | ||
− | + | A vector [[Scripting terminology#literal|literal]] consists of one to three float values separated by commas and enclosed in square brackets. | |
− | + | ||
− | A vector literal consists of one to three float values separated by commas and enclosed in square brackets. | + | |
<dascript> | <dascript> | ||
Line 27: | Line 23: | ||
</dascript> | </dascript> | ||
− | Note although there is a literal, a vector cannot be declared as a constant using the [[const]] keyword. | + | Note although there is a literal, a vector cannot be declared as a constant using the [[Const keyword|const]] keyword. |
== Conversion == | == Conversion == | ||
Line 96: | Line 92: | ||
[[:Category:Vector functions|Vector functions]] | [[:Category:Vector functions|Vector functions]] | ||
− | [[Category: | + | [[Category:Keywords]] |
Latest revision as of 18:17, 13 August 2011
The vector type represents a position or direction in three dimensions. It consists of three float values representing x, y and z which are accessed using the "dot operator". The default value is (0.0, 0.0, 0.0).
Constructor
The constructor for a vector is the Vector function.
Literals
A vector literal consists of one to three float values separated by commas and enclosed in square brackets.
// valid vector literals vector vLiteral1 = [1.0]; vector vLiteral2 = [1.0, 2.0]; vector vLiteral3 = [1.0, 2.0, 3.0]; // invalid vector literals vector vInvalid1 = [1.0, , 3.0]; // causes "Parsing constant vector" error vector vInvalid2 = [1.0, 2, 3.0]; // causes "Parsing constant vector" error vector vInvalid3 = 1.0, 2.0, 3.0; // causes "Parsing variable list" error vector vInvalid4 = (1.0, 2.0, 3.0); // causes "Unknown state in compiler" error
Note although there is a literal, a vector cannot be declared as a constant using the const keyword.
Conversion
Explicit
The following functions can be used to convert another data type to a vector:
The following functions can be used to convert a vector to another data type:
Implicit
There is no implicit conversion to a vector.
Persistence
There are no native functions providing persistence for a vector, however derived functions can be created using GetLocalFloat and SetLocalFloat to store and retrieve the individual components.
// Gets three floating point variables from the specified object and returns them as a vector. vector GetLocalVector(object oObject, string sVarName) { float fX = GetLocalFloat(oObject, sVarName + "x"); float fY = GetLocalFloat(oObject, sVarName + "y"); float fZ = GetLocalFloat(oObject, sVarName + "z"); return Vector(fX, fY, fZ); } // Sets three floating point variables, representing a vector, on the specified object. void SetLocalVector(object oObject, string sVarName, vector vValue) { SetLocalFloat(oObject, sVarName + "x", vValue.x); SetLocalFloat(oObject, sVarName + "y", vValue.y); SetLocalFloat(oObject, sVarName + "z", vValue.z); }
As with all Get|SetLocal* functions the name of the variable must be predefined in the object's variables 2da file. In this instance all three variables (with the appropriate suffix) must be predefined.
Examples
void main() { // uninitialised: value = (0.0, 0.0, 0.0) vector vDefault; // initialised using a literal: value = (1.0, 2.0, 3.0) vector vSpecific = [1.0, 2.0, 3.0]; // initialised using the constructor: value = (1.0, 2.0, 3.0) vector vConstructed = Vector(1.0, 2.0, 3.0); // initialised using a function: value = (-1.0, 0.0, 0.0) vector vConverted = AngleToVector(90.0); }