This guide is aimed at people who never or barely were in touch with coding. It will help you with writing your first simple scripts in Space Engineers. If you search for an advanced guide I recommend this one from Malware.
Disclaimer
English isn’t my first language so please don’t expect a linguistical masterpiece. Also i never learned coding/programming and i built my knowledge through google research and annoying friends with stupid questions about coding in C#. Thus this guide may contain wrong terms or minor mistakes. But i’m sure that this guide will enable anyone without any knowledge about scripting to create simple scripts for Space Engineers.
Example Script
I think the best way to learn scripting in Space Engineers is via examples. We will make a rotor move to a certain point in this first one.
At the beginning we need to tell the game which block we want to adress. We will save the block into a variable to make the script more readable. Therefore let’s create a variable for our rotor:
Every variable needs a type (“MyMotorStator”) and a name (“Rotor”). The type tells which value we can save into the variable. This value could be a number or just text but in our case it’s a rotor block. The type for this is “MyMotorStator”. We can simply choose any name but it is advised to take one that makes the script easy to read and understand, especially when it gets more complex.
Every line of code (like a sentence) needs a semicolon “;” at its end. This is easy to forget but just one missing semicolon makes whole script not working as intended.
Now we will give the variable its value: The rotor block. In our case we just say the rotor block is named ‘Super mega station rotor’
The type only gets mentioned the first time we create a variable. To give a variable a value we simply use the “=” symbol.
This part is the method to adress the block in our grid. We simply write the exact name of the block between the two “”. Don’t worry about the text too much, we’ll come back to that later. At the end we write the type again in order to make sure the block that is getting assigned to our variable has the correct type. If you forget this and there is for example an LCD panel named “Super mega station rotor” it would get assigned to the variable. This may sound not necessary but just use it to have a safer script.
Now every time we use the variable ‘Rotor’ in our script the game adresses our rotor block.
Let’s move the rotor to a certain point now. We want it to rotate to 45 degrees with a positive velocity. So we have to do 2 things. The first one is to set its maximum angle to 45 degrees. The second one will be to give the rotor its velocity.
Let’s start with step one:
At first we tell the game we want to use the rotor block. Then we write a “.” and add the property or method we want to use. This property needs a value to set the rotors maximum limit to. So we write “45”. The “f” is the type of the value (in this case a number, but i will come back to that later)
Let’s come to the velocity setting. This works quite similar just the property is different. We set the velocity to 1 in this case.
And that’s it. Once the script is run, the rotor will turn with 1rpm until it hits 45 degrees.
The whole script would look like this:
I’ll summarize it:
At first we save the block(s) into a variable in order to have a more readable script and easier use. (You could also just write it like this:
But i think it is obviously nicer to use variables instead.
Then we can use properties and methods to change the settings of the blocks.
The basics needed for coding in Space Engineers
Don’t worry, you don’t have to immediately remember everything listed here, I will add a chapter with a list of everything you could need. But in this part I will try to explain these basic things needed for nearly every script.
“//” and “/* */” mark comments in your script. These will be ignored when executed. This means you can explain something or just give a headline to your code. “//” written in front works for the current line and “/* */” encloses the comment. Commenting helps understanding a script and shouldn’t be underestimated.
Mathematical and logical operators
Let’s start with simple mathematical and logical operators. This should be a piece of cake
I’ll just list the most important ones.
“+”: Addition
“-“: Subtraction
“*”: Multiplication
“/”: Division
“%”: Modulo; this will return the remainder of a division. Examples: (5 / 2 = 2; remainder = 1) => 5 % 2 = 1; 7 % 4 = 3;
“==”: Equals; this is used for comparing values.
“!=”: Not equal; basically the opposite of “==”.
“>”, “>=”, “<“, “<=”: Greater/smaller than (or equal to); Also used for comparing values.
“&&”: And; all the variables connected with this have to match in your comparison
“||”: Or; at least one of the variables connected with this has to match in your comparison
“=”: In scripts this is mainly used to assign values.
Variable types
Let’s continue with variable types:
“bool”: This type of variable can only have one out of these two values: It’s either “true” or “false”.
“string” : This is just plain text. An example would be to save text into a variable to display it on an LCD screen later.
“int” : This is a number but without any decimal places. The value doesn’t get rounded; instead all decimal places will be just cut off. A 4.9 saved as “int” is 4.
“float” : This is also a number but it has decimal places.
“double”: The third kind of number. It’s similar to “float” but the number can be larger. This won’t be that important for basic scripting. I’d advise to use float unless a method requires double as input. If you assign a number value in your script it’s usually treated as “double”. To be treated as “float” you have to write an “f” behind the number, like “4.9f”.
“IMyMotorStator”: This should sound familiar. It is a block type from Space Engineers. There are many different ones and we will ignore it for now.
To initialize (create) a variable we just write the type and the variable name and end with a semicolon:
We can give it a value after this:
We can also do these two steps at once:
It is possible to calculate with the three number types “int”, “float” and “double”.
Important: Saving a number into a “string” works but you can’t use mathematical operators on it since it’s seen as text. You can imagine it like calculating with letters.
Advanced logical operators
I’ll assume that I don’t need to explain how the basic true/false logic system works. But if somebody needs help with that, just write a comment.
Let’s explain the use of more advanced logical operations.
If-function
Let’s start with the “if-function”. It compares 2 parameters and only executes the included code if the comparison is correct/true. I think the easiest way of explaining it is again via examples.
Let’s say we have an already rotating rotor and when it’s at 30 degrees a light should switch on.
As in our first script we start by saving our blocks into variables.
Let’s begin with the “if-function”.
This is the head. The argument is written between the “()”. This property needs the angle to be in radiant. 0.5236 rad converted in degrees is 30.
This is the body. It contains the code between the “{}”. It encloses an area of code that belongs to the function written in front of it.
At first this function will check if the argument in the “()” is true and then execute the code between the “{}” if the argument is correct. If not, it will ignore the code between the “{}”.
We can also ‘upgrade’ the “if-function” with an “else”. This allows us to have code executed in the case the argument is wrong. If the statement in the “if-function” is true, the “else” will be ignored.
In this example we want the light to switch off if the rotor is not at 30 degrees.
Lastly you can also add an “if else”. It will act as an “else” but it also has a condition which must be true to execute its code. Let’s say we want have 4 lights in different colours and we want a specific light to be turned on for the four quarters of the rotor angle.
At first we have to create variables for our new lights.
Let’s start building our “if-function”
This checks if the angle is 0 or above but also under 90 degrees (1.571 rad). Both conditions must be met for the statement to be true. If you would is the “Or-operator” (“||”) instead of the “And-operator” (“&&”) only one of the conditions has to be true for the statement to be true.
The green light turns on, all other lights turn off.
We will repeat this for the other quarters now.
3.142 rad equals 180 degrees. We don’t have to check if it is above 90 degrees because this “if else” will only be checked if the “if” condition is false and thus the angle is above 90 degrees.
4.712 rad equals 270 degrees.
For the last case we don’t have to use an “else if” because the only angles left are those we want in our condition anyway (270-360 degrees).
Switch
The next function we can use is the “switch”. It’s pretty similiar to a “if” and “if else” constellation but all your conditions need to be based on only one value of one variable. Furthermore you can only compare it with a “==”. So it wouldn’t work for our last example since we needed ‘smaller than’ (“<“) for our conditions.
In the following example we change it to the conditions being an exact angle and not an area. So let’s say we want 4 cases. 0, 90, 180 and 270 degrees. Every angle will turn one of our lights on and all the other ones off.
This is the head. Between the “()” the value that we want to check is written. This can also be a variable of course.
The angle gets compared with the “0”. You can imagine this as an “if (Rotor.Angle == 0)”.
The break will make the script jump out of the whole switch after the correct case has been found. This is important to save execution time, since there won’t be time wasted on checking all the other cases.
This is optional. This case gets used when no other cases match the condition. In our example this gets used when the angle is in-between 0, 90, 180 and 270 degrees but not one of these exact numbers. We can just turn all lights off for example.
For-loop
The last one for now is the “for loop”. It is used to repeat the same code for a group of variables. In order to create that group of variables we need to learn about the “list”. It creates a group of the objects that you put into the list; for example all light blocks on a grid. Creating a new list slightly differs from creating a variable:
We have created a list with the name “Lights”. Also the list needs a type, so this means a list can’t contain objects of a different type. In this case our type is “IMyInteriorLight”. This is the type of light blocks in Space Engineers but you can also make a list of numbers with the type “int”.
Now let’s fill the list with all light blocks on our grid:
This puts all blocks from the group “Super mega station lights” into the list mentioned between the “()” at the end.
To address a single object in our list we use this phrase: “Lights[0]”. Every object in the list has a number to which it responds. This always starts at “0” for the first object and is counted upwards. If you create a list with 20 objects the first object will respond to “0” and the last one will respond to “19”. The number written between the “[]” addresses the individual object. To turn the third light on we would write it like this:
The list is set up, so let’s proceed with the “for loop”:
This is the head. Between the “()” we have 3 different parameters separated by a semicolon. The first one (“int i = 0”) creates a variable named “i” with a value of “0”. The third one (“i++”) just adds “1” to our variable “i” every time the loop is executed. The second parameter (“i < Lights.length”) defines when the loop should end. This means the loop gets executed as long as “i” is lower than a specific value. If you would write “i < 20” the loop would just get executed 20 times. In this situation I used Lights.length, which gives us the amount of objects that the list “Lights” contains. This basically means the “for-loop” gets executed as often as the amount of objects in our list.
This is the body of the “for-loop”. By using the variable “i”, we start with the first object (“0”) in our list. Since we add “1” to “i” with every loop, we also progress through our list of objects by 1. Thus at the end of the “for-loop” every light on the list got turned on.
These advanced logical operators should help you when creating your own scripts.
Properties and methods
This might be one of the hardest steps for a beginner. There is a huge amount of different blocks in Space Engineers and thus there are many different block types, methods and properties. If you want to know the difference between properties and methods, you would have to ask someone else, since I don’t really know it but that doesn’t really matter.
The best way to work with them is to look it all up in this
[github.com]while you’re scripting something. Huge credits to Malware who created it. There you can find all block types and their methods and properties (basically what you can do with the block). There are a few things you need to know in order to use these.
In order to use a property/method you have to write your block variable, then add a “.” and then write your property/method.
Most Properties can either get or set a value (some only get values). You decide this by either giving it a value by adding something like ” = value” or not. Here are two examples to give you an idea how this works:
Getting a value:
This saves the angle into our variable “angle”.
Setting a value:
This enables the rotor lock.
In the guide you will also see which type the property/method gives back or needs as an input. This is very important. The property “RotorLock” needs a “bool” which means it only accepts “true” or “false” as in- or output.
Writing your first script
If you open the programmable block you will see this:
We will ignore the “Program” and “Save” function for now. Your whole code will be placed between the two “{}” of the “Main” function. This is the part that gets executed when the script is used. If you would implement the first script from the beginning of this guide, it would look like this:
I highly advise you to write your scripts outside of Space Engineers. The best way for me is using Visual Studio together with Malware’s extension. It provides many features that you will love during coding. A guide on how to install and use it is also found
[github.com]in Malware’s guide.
Cheat Sheet
Mathematical and logical operators:
“+”
“-”
“*”
“/”
“%”
“==”
“!=”
“>”, “>=”, “<“, “<=”
“&&”
“||”
“=”
Value types:
“bool”: true/false
“string”: text
“int”: number without decimal places
“float”: number with decimal places
“double”: larger number
“list”: group of objects
“IMyInteriorLight”: example of a block type
Advanced logical operators:
Properties/Methods:
That's everything we are sharing today for this Space Engineers guide. This guide was originally created and written by 33iRobot33. In case we fail to update this guide, you can find the latest update by following this link.