Difference between revisions of "Vector keyword"
m (Fixing category) |
m |
||
Line 96: | Line 96: | ||
[[:Category:Vector functions|Vector functions]] | [[:Category:Vector functions|Vector functions]] | ||
− | [[Category: | + | [[Category:Keywords]] |
Revision as of 23:38, 2 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
A constructor is a function that creates an instance of a dascript type.
The constructor for a vector is Vector
Literals
A literal is a textual representation of a particular value of a type.
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); }