Merlin of Mines - Space Engineers

A set of useful scripts for aspiring Space Engineers!

View on GitHub

Operations

Operations allow you to manipulate and combine Variables together using addition, subtraction, etc. The result of an Operation always returns another Variable, so you can chain operations together:

set a to 1
set b to a + 1
set c to a + b + 1

There are a few types of Operations:

UniOperations include operations like not, abs, sin, cos, tan etc. Note that some UniOperands expect the operand to preceed the operations (such as keys, and values)

BiOperations include things like “+”, “-“, “*”, “/”, “%”, etc.

Below is a description of the various operations supported by EasyCommands, including the behavior for different Primitive Type input(s) and output type.

One final note: In general, operations that produce a collection typically start with a collection (with a couple of exceptions). For more information on how you can work with collections of things in EasyCommands see Collections.

Order of Operations

Operations happen in “tiers”, meaning some operations are performed before others. For example, multiplication & division are attempted before addition and subtraction. Note that addition does not always happen before subtraction; they are the same tier of operation.

set result to 1 + 2 * 3
Print "Result: " + result
#Result: 7

Also, all UniOperands are performed before other operands.

set result to abs -1 + 2
Print "Result: " + result
#Result: 3

All UniOperand Operations are evaluated before BiOperand Operations.

Here are the tiers for BiOperand Operations:

Changing Order of Operations

To change the order of operations, use parentheses (). Anything in parentheses is evaluated first. Order of operations within the parantheses follows the same rules as outside. You can nest parentheses to further change order of operations.

set result to (1+2)*3
Print "Result: " + result
#Result: 9
set result to abs (-1 + 2)
Print "Result: " + result
#Result: 1
set result to abs (-2 * (1 + 3))
Print "Result: " + result
#Result: 8

Casting Variables To Other Types

Sometimes you may want to convert one input type to another, for example, a string input which is actually supposed to be a number. You can do this using the Cast Operation.

Keywords: cast, as, resolve, resolved

The Cast operation can be used as either a UniOperand or as BiOperand.

Casting As a UniOperand

When used as a UniOperand, the Cast operand will attempt to parse a given string variable as another variable type, by looking at its structure. Here are some Examples:

set myBool to cast "true"
set myNumber to cast "3.14"
set myVector to cast "1:2:3"
set myColor to cast "red"
set myColor to cast "#FF0000"
set myString to cast "Some random String"

Casting strings to lists is not currently supported. If the input variable is not a string, it is returned without any conversion. So casting a vector does nothing.

See the Cast under UniOperands for more information.

Casting as a BiOperand

When used as a BiOperand, the Cast Operation expects the second variable to be a string representing one of the supported cast types.

See Cast under BiOperands below for more information.

The Minus Sign

- can be used as either a UniOperand to negate a value (See the Not Operation)), or as a BiOperand to subtract a value from another value (see the Subtraction operation).

#Negation
set a to 2
set b to -a
print "b: " + b
#b: -2
#Subtraction
set a to 2
set b to 5 - a
print "b: " + b
#b: 3

EasyCommands will do its best to infer whether you meant by “-“, but may not be perfect. If needed, use parantheses to clarify your intent.

Negation resolves before subtraction, so -a-a resolves to (-a) - a, vs -(a-a).

Rounding

Rounding can be used to either quickly round number and vector values to an integer, or to round numbers and vector values to a specific number of digits.

Keywords: round, rnd, rounded

Note that “digit and “digits” are ignored keywords.

If the digits to round to are not specified, rounding takes precedence over other BiOperands like *,+ etc. When the number of digits are specified, then Rounding acts as a tier 1 BiOperand.

Here’s some examples for rounding numbers:

print pi rounded to 2 digits
#3.14

print "Decimal: " + pi rounded 1
#Decimal: 3.1

print "Integer: " + 9.81 rounded
#Integer: 10

set myValue to 5.617
print "My Value: " + round myValue
#My Value: 6

And here’s some examples for rounding vectors:

print "1.11:2.22:3.33" rounded to 1 digit
#1.1:2.2:3.3

print "1.11:2.22:3.33" rounded
#1:2:3

print "My Cockpit" velocity rounded to 2 digits

Uni Operations

Absolute Value

Behavior varies based on input type.

Keywords: abs, absolute

Arc Cosine

Performs the arc cosine operation on the given numeric value

Keywords: acos, arccos

Arc Sine

Performs the arc sine operation on the given numeric value

Keywords: asin, arcsin

Arc Tangent

Performs the arc tan operation on the given numeric value

Keywords: atan, arctan

Cast (Before or After)

Casts the given string variable to another variable type by attempting to resolve it to the appropriate type.

Keywords: cast, resolve, resolved

#number
set myVariable to cast "123.4"

#Vector
set myVariable to "1:2:3" resolved

#Color
set myVariable to resolve "#FF0000"

If the input type is not a string, will return the original value. If the input string cannot be parsed as a supported Primitive Type, the original string will be returned.

#Returns the original string
set myVariable to resolve "Some Random String"

Cosine

Performs the cosine operation on the given numeric value

Keywords: cos, cosine

Keys (After)

Gets the keys from the given list. The list is expected to come before the operand. Items without keys are not returned in the resulting list.

Keywords: keys, indexes

set myList to ["one" -> 1, "two" -> 2, 3]
#myKeys will be ["one","two"]
set mykeys to myList keys

Natural Logarithm

Returns the logarithm of the given numeric value to the base of the mathematical constant e

Keywords: ln

Not

Inverses a given property. Behavior varies by input type

Keywords: not, !, isnt, arent, stop, -

Example:

set myValue to true
set myNewValue to not myValue

Not can also be used in some property conditions.

if the "Gatling Turrets" are not firing
  Print "Why arent you firing??"

Random

Behavior varies based on input types.

Keywords: random, rand

#Random number between 0 and 9
print "Random Number: " + rand 10

#Random item from list
Print "Random Item: " + rand [one,two,three,four,five]

#Random number between 5 and 10 using List
print "Random Number: " + random (5..10)

Reverse

Reverses the given list

Keywords: reversed

set myList to ["one", "two", "three"]
set myReversedList to reversed myList

Round (before or after)

Behavior varies based on input types.

Keywords: round, rnd, rounded

Shuffle

Shuffles the given list. This does not modify the input list but rather returns a shuffled copy of the input list.

Keywords: shuffle, shuffled

Sign

Behavior varies based on input type.

Keywords: sign, quantize

Sin

Performs the sin operation on the given numeric value

Keywords: sin

Square Root

Returns the Square Root of the given number

Keywords: sqrt

Sort

Sorts the given list using the comparison operand against each value (see below). This operation is expensive so be careful when using it.

Keywords: sorted

set myList to [3,4,1,2]
#Becomes [1,2,3,4]
set myReversedList to sorted myList

Tangent

Performs the tangent operation on the given numeric value

Keywords: tan, tangent

Ticks (After)

Converts the given number from seconds to ticks

Keywords: tick, ticks

Example:

#Wait 30 ticks = 0.5 seconds
wait 30 ticks

#Wait 30 seconds
wait 30

Type (After)

Returns the Primitive Type of the given variable as a string.

Keywords: type

Possible output values: boolean, string, number, vector, color, list

set myVariable to "True"
print myVariable type
#bool

#Notice the quotes
set myVariable to "123"
print myVariable type
#string

set myVariable to 123
print myVariable type
#number

set myVariable to 1:2:3
print myVariable type
#vector

set myVariable to #FF0000
print myVariable type
#color

set myVariable to [1,2,3]
print myVariable type
#list

This is particularly useful when trying to cast arbitrary input as a value.

#Pretend the text is "1"
set myInput to "My Display" text

#This will resolve the input string to a primitive type but you don't know which
set myResolvedVariable to resolve myInput

#This will tell you what type you are dealing with
set myVariableType to myResolvedVariable type
print "Type: " + myVariableType
#number

if myVariableType is number
  set myNewValue to myResolvedVariable + 10
  print myNewValue
  #11
else
  set "My Display" text to "Input must be a number! Try again"

Values (After)

Gets the values from the given list (ignoring keys). The list is expected to come before the operand.

Keywords: values

set myList to ["one" -> 1, "two" -> 2, 3]
#myValues will be [1,2,3]
set myValues to myList values

BiOperand Operations

Addition

Behavior varies based on input types.

Keywords: plus, +

And

Checks whether both boolean operands are true (a && b)

Keywords: and, &, &&, but, yet

Cast

This special operation allows you to cast a given value as another value. This enables you to construct something using a string, and then convert it to a vector or color, for example.

Keywords: cast, as

Cast Types: bool, boolean, string, number, vector, color, list

set myVector to 1 + ":" + 2 + ":" + 3
set myVector to myVector as vector
set the "Remote Control" destination to myVector

If you attempt to cast a variable to a type that it cannot be converted to, you will get a script halting exception. So before casting to a specific type make sure you know you can cast it to that type.

If you are unsure of the type, use the “Cast” UniOperand to cast it to whatever type it actually is, and then check its type using the Type operation before converting.

#Pretend I don't know what this value is
set myInputValue to "My Display" text

set myResolvedValue to cast myInputValue
if myResolvedValue type is not number
  Print "Input Value must be a number!"
else
  Print "Input Number is: " + myResolvedValue

Supported Casts

Boolean

Number

String

Vector

Color

List

List has no supported conversion types currently

Comparison

There are a few different comparisons supported by EasyCommands. Comparison support and behavior changes slightly based on the input types, see below for description.

Equals

Keywords: is, are, equal, equals, =, ==

Not Equals

Keywords: is not, are not, !=, is not equal, not equal

Less Than

Keywords: less than, <, below

Greater Than

Keywords: greater than, >, more than, above

Greater Than Or Equal

Keywords: >=

Contains

Checks whether the first operand contains the given value. (a contains b). Behavior varies based on input types.

Keywords:

contains

Examples:

#True
set containsValue to "My Value" contains "Value"

#False
set containsValue to "My Value" contains "Me"
set myList to ["one", "two", "three"]
#True
set containsValue to myList contains "one"
set containsValue to myList contains ["one", "three"]

#False
set containsValue to myList contains 1
set containsValue to myList contains ["one", "four"]

Division

Behavior varies based on input types.

Keywords: /, divide

Dot Product

Returns the dot product of the given two vectors.

Keywords: .

Accessing Vector and Color Components

The dot product is used in a clever way in order to get component information for vectors and colors.

You can use myVector.x, myVector.y, myVector.z to get the components of a given vector.

Also, you can use myColor.r, myColor.g, myColor.b to get the RGB components of a given color.

See the Vector and Color Primitives for more information.

Exponent (Also XOR)

Behavior varies based on the input types.

Keywords: ^, pow, xor

Join

Joins a given list by the given string separator by casting each list value to a string and then joining them together using the given separator. When joining values the keys are stripped. Only the values get joined.

Keywords: join, joined

set myOutput to [1,2,3] joined ", "
#1, 2, 3
#Line Separated Values
set myOutput to [1,2,3] joined "\n"
print myOutput
#1
#2
#3
#Keys are stripped
set myOutput to ["one" -> 1, "two" -> 2, "three" -> 3] joined ", "
print myOutput
#1, 2, 3

Modulus

Behavior varies based on input types.

Keywords: %, mod

Multiplication

Behavior varies based on input types

Keywords: *, multiply

Or

Checks whether either boolean operand is true (a || b).

Keywords: or, |, ||

Range

Creates a Collection consisting of numbers between a and b (inclusive).

This operation is often used for iterating through existing collections by index, or fetching a subset of values from a given collection. See Collections for more information on how to use the Range operation to perform various operations on a given Collection.

Keywords: ..

Example:

#[0, 1, 2]
set myList to [0..2]

set myList to ["one", "two", "three"]
for each i in 0..count of myList[] - 1
  Print "myList[" + i + "]: " + myList[i]

Round

Behavior varies based on input types.

Keywords: round, rnd, rounded

Split

Splits the given string by the given string separator. The result is a List containing the separated values.

Keywords: split, separate, separated

set myOutput to "My Values" split " "
#[My,Values]
#Get Display Output Lines
set myOutput to my display[0] text split "\n"

Subtraction

Behavior varies based on input types.

Keywords: -, minus

Ternary Operations

There’s only one supported ternary operations, currently. It uses the first operand as a conditional check. If the check is true, it returns the 2nd operand; otherwise, it returns the 3rd operand.

The format is: conditionVariable ? trueVariable : falseVariable

set myCondition to true
set myValue to myCondition ? "True!" : "False!"

Print myValue
#True!

set myCondition to false
set myValue to myCondition ? "True!" : "False!"

Print myValue
#False!