Meteor 2 Scripting Functions
Math
int randomInt(int lower, int upper)
Generate a int between lower and upper (both inclusive).

Example
print(randomInt(1, 10));

See also
randomFloat randomDir randomArray

float randomFloat(float lower, float upper)
Generate a random float between lower and upper (both inclusive).

Example
print(randomFloat(0, 1));

See also
randomFloat randomDir randomArray

float randomDir()
Generate a random direction/angle.

Example
print(randomDir());

Comments
Same as calling randomFloat(0, 360);

See also
randomFloat randomInt randomArray

int/string randomArray(array inputArray)
Pick a random element from an array.

Example
array<int> numbersArray = {1,1,1,2,3,4};
print(randomArray(numbersArray)); // 1 is most likely result

array<float> floatsArray = {1.0,1.1,1.2,1.3,1.4,1.5};
print(randomArray(floatsArray));

array<bool> boolsArray = {true,true,true,false}; // true is most likely
print(randomArray(boolsArray));

array<string> fruitArray = {"Apple", "Banana", "Orange", "Orange", "Orange"};
print(randomArray(fruitArray)); // orange is most likely result

array<vector2> vector2sArray = {vector2(1,1),vector2(1,2),vector2(1,3)};
print(randomArray(vector2sArray));


Comments
Supports int, float, bool, string and vector2 arrays.

See also
randomFloat randomInt randomDir

vector2 getRotatedSize(vector2 size)
Get the bounds size of a rectangle rotated in degrees.

Example
print(getRotatedSize(vector2(5,10), 90)); // 10, 5

See also
rotatePoint

vector2 rotatePoint(vector2 point, angle float, vector2 centrePivot)
Rotate a point around a pivot.

Example
// rotate 0,50 270 degrees around 50,50
vector2 result = rotatePoint(vector2(0, 50), 270, vector2(50, 50));
print(result); // 50, 100

See also
getRotatedSize

float pixelsToMetres(float pixels)
Convert pixels to metres.

Example
print(pixelsToMetres(256)); // 16

Comments
16 pixels is 1 metre.
The alternate spelling "pixelsToMeters" can also be used.
A global constant PIXELS_PER_METRE (or PIXELS_PER_METER) is also available e.g. float met = pix / PIXELS_PER_METER;

See also
metresToPixels

float metresToPixels(float metres)
Convert metres to pixels.

Example
print(metresToPixels(16)); // 256

Comments
16 pixels is 1 metre.
The alternate spelling "metersToPixels" can also be used.
A global constant PIXELS_PER_METRE (or PIXELS_PER_METER) is also available e.g. float pix = met * PIXELS_PER_METER;

See also
pixelsToMetres

float getBearingTo(vector2 fromPos, vector2 destPos)
Get the bearing from fromPos to toPos in the range 0-360.

Example
float bear = getBearingTo(getPos(PLAYER_OBJECT), vector2(0,0));
print("Bearing to top left of map is: " + bear);

See also
moveForwardOnBearing

vector2 moveForwardOnBearing(vector2 pos, float bearing, float dist)
Move a position along bearing and return the result.

Example
// Teleport player 10M forward
vector2 newPlayerPos = moveForwardOnBearing(getPos(PLAYER_OBJECT), getDir(PLAYER_OBJECT), metresToPixels(10));
setPos(PLAYER_OBJECT, newPlayerPos);

See also
getBearingTo

float getDistance(vector2 pos1, vector2 pos2)
Get the distance between 2 points in pixels.

Example
print("Distance to top left of map is: " + getDistance(getPos(PLAYER_OBJECT), vector2(0,0)) + " pixels");

See also
getDistanceMetres

float getDistanceMetres(vector2 pos1, vector2 pos2)
Get the distance between 2 points in metres.

Example
print("Distance to top left of map is: " + getDistanceMetres(getPos(PLAYER_OBJECT), vector2(0,0)) + "M");

Comments
The alternate spelling "getDistanceMeters" can also be used.

See also
getDistance

float clamp(float value, float minValue, float maxValue)
Clamp a number between two values.

Example
print(clamp(-9999, 0, 100)); // 0
print(clamp(9999, 0, 100)); // 100
print(clamp(50, 0, 100)); // 50


Index

Generated on the 23 November 2024 at 08:20:42 (UK Time)