Meteor 2 Scripting Functions
Math
int randomInt(int lower, int upper, bool useSeed=false, uint seed=0)
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, bool useSeed=false, uint seed=0)
Generate a random float between lower and upper (both inclusive).

Example
print(randomFloat(0, 1));

See also
randomFloat randomDir randomArray

float randomDir(bool useSeed=false, uint seed=0)
Generate a random direction/angle.

Example
print(randomDir());

Comments
Same as calling randomFloat(0, 360);

See also
randomFloat randomInt randomArray

float randomSeed()
Generate a random seed value.

Example
print(randomSeed());

int/string randomArray(array inputArray, bool useSeed=false, uint seed=0)
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

float getPerlinSample(vector2 pos, uint seed)
Generate a value based on a 2D vector and a seed using perlin noise.

Example
float value = getPerlinSample(vector2(0.5,0.5), getWorldSeed());
print(value);

See also
getWorldSeed();

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 rotateTowards

vector2 rotateTowards(float angle, float targetAngle, float step)
Rotate angle towards target angle.

Example
print(rotateTowards(20, 90, 10)); // 30

Comments
All values are in degrees (0 to 360).

See also
moveForwardOnPlane

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

float min(float a, float b)
Return the smallest of two values.

Example
print(min(1, 10)); // 1

See also
max clamp

float max(float a, float b)
Return the largest of two values.

Example
print(max(1, 10)); // 10

See also
min clamp

float lerp(float a, float b, float t)
Linear interpolate a value between a and b by the normalised fraction t.

Example
print(lerp(1.0, 2.0, 0.5)); // 1.5

float fmodf(float a, float b)
Return the fraction remainder of x/y.

Comments
Same as fmodf() in C.

uint createStringHash(string stringValue)
Generate a hash value from a string (string is case sensitive).

Example
unsigned hash = createStringHash("Hello World!");
print(hash);

See also
createStringHashLowerCase

uint createStringHashLowerCase(string stringValue)
Generate a lower case hash value from a string (string is automatically converted to lower case).

Example
unsigned hash1 = createStringHashLowerCase("Hello World!");
print(hash1);
unsigned hash2 = createStringHashLowerCase("hELLO wORLD!");
print(hash2);

See also
createStringHash


Index