Space Engineers – How to program for beginners

Space Engineers – How to program for beginners 1 - gameplaylists.com
Space Engineers – How to program for beginners 1 - gameplaylists.com

This guide was created for people who were not or only barely familiar with coding. It will help to create your first Space Engineers script. I recommend it to anyone looking for an advanced guide.
This one [github.com]Malware.
 
 

Disclaimer

English is not my first tongue so don’t expect me to be a linguistic master. I don't know programming/coding. I learned it through Google research and annoying my friends with stupid questions. This guide may include mistakes or misspellings. However, this guide is sure to allow anyone without scripting knowledge to create simple programs for Space Engineers.
 
 
 

Example Script

I believe that the best way of learning Space Engineers scripting is through examples. This first rotor will be moved to a point.
 
At the beginning, tell the game what block you want to address. To make the script easier-to-read, we will save the blocked into a variable. Let's create an variable for our rotation:
 
 

MyMotorStator Rotor; 

 
 
Every variable requires a type name ((MyMotorStator)). The type specifies the value that can be saved to the variable. This value could be text or a numerical number. In this case, it is a rotorblock. This value is "MyMotorStator". You can choose any name. However, it is best to pick one that is easy for you to read and understand.
 
Every line in code (, just like a sentence in), must have a semicolon (;) at its end. This is simple to forget but if you miss a semicolon, the entire script will not function as intended.
 
 
Now, we'll give the variable it's value: The "rotor block". In our example, we'll just call the rotorblock "Super mega station rotation".
 
 

Rotor = IMyTerminalGrid.GetBlockWithName("Super mega station rotor") as IMyMotorStator; 

 
 
The first time you create a new variable, the type is mentioned. Simply use the "=" symbol in order to give a varible a value.
 
This is how we address the block of our grid. We simply need to write the exact block name between the two "". We won't spend too much time on the text. We'll come back later. To make sure that the block that will be a*signed to our variable's variable has the correct type, we must write it again at the end. If you forget to do this, the LCD panel "Super mega station rotation" would be a*signed. This may sound redundant but it's necessary in order to have a safer code.
 
Now, everytime we use the variable Rotor in our script the game addresses our block of rotor.
 
 
Let's now move our rotor towards a desired point. We want it rotate at 45 degrees and with a positive velocity. Two things are needed. The first is to make the maximum angle 45 degrees. The second is to increase its velocity.
 
Let's start with step 1.
 
 

Rotor.UpperLimitDeg = 45f; 

 
 
First, tell the game you want to use a rotor block. Next, we write a "." Then add the property/method you want to use. This property should have a specific value to be able to set the maximum limit on rotors. So we write "45". The "f", or type of the (value in this case is a number. However, i'll return to the) later.
 
 
Let's get to the velocity setting. This works exactly the same, but the property of the velocity setting is different. In this case, the velocity is set to 1.
 
 

Rotor.TargetVelocityRPM = 1f; 

 
 
That's all. Once the script was run, the Rotor will turn at 1 RPM until it reaches 45 Degrees.
 
The whole script would look like the following:
 
 

MyMotorStator Rotor;
Rotor = IMyTerminalGrid.GetBlockWithName("Super mega station rotor") as IMyMotorStator;
Rotor.UpperLimitDeg = 45f;
Rotor.TargetVelocityRPM = 1f; 

 
 
I'll sum it up:
 
To make it more readable, we save the block(or) into variable. (Or you could just write it like the following:
 
 

IMyTerminalGrid.GetBlockWithName("Super mega station rotor ").UpperLimitDeg = 45f;

 
 
It is, however, much more pleasant to use variables.
 
Then, we can use properties or methods that allow us to change the settings on the blocks.
 
 
 

The basics for Space Engineers Coding

Don't worry. You don't necessarily have to remember everything. I will add an additional chapter with a list. I will explain the essentials for nearly all scripts in this part.
 
 
"//" & "/*////" are comments to your script. These will be ignored once the code is executed. This means that you can explain your code or just give it a headline. "//" in front works for the current lines and "/**//" wraps the comment. Commenting aids in understanding scripts and should not to be underestimated.
 
 
 

Mathematical or logical operators

Let's get started with simple mathematical or logical operators. This should be simple.
 
I'll only list the most important.
 
"+": Supplement
 
"-": Subtraction
 
"*": Multiplication
 
"/": division
 
"%":Modulo. This will return a remainder of a division. Examples: (5/2 = 2 and remainder = 1 – =>5 %2 = 1 and 7 %4 = 3
 
 
"==": Equals; this can be used to compare values.
 
"!=": Is not equal to "==".
 
">", HTML4_). Also used as a way to compare values.
 
"&&": Also, all variables related with this have to match your comparison
 
 
 
"=": This is used primarily to a*sign values to scripts.
 
 
 

Variable types

Let's keep going with variable types.
 
"bool”: This type is limited to one of the two values. It can have either "true", or "false".
 
 
"string" – This is just text. You could, for example, save text to a variable to display it on an LED screen later.
 
 
"int", a number that doesn't include any decimal places, is an example of a number. The value won't get rounded. Instead, all the decimal places will just be cut off. A 4.9 saved to "int" will be 4.
 
 
"Float” : This can also be a number but has decimal places.
 
 
"Double": The third type of number. It's a similar concept to "float", except that the number may be larger. This isn't important for basic scripting. I would advise using float unless you need to input doubles. It is usually considered "double" when you a*sign a number to your script. To be considered "float", a "f" must be added to the number. For example, "4.9f".
 
 
"IMyMotorStator",: This should be familiar. It's a block type from Space Engineers. There are many types and we will ignore it.
 
 
To initialize (simply write) and the variable's name, then finish with a semicolon.
 
 

int a;

 
 
We can give it an additional value.
 
 

a = 1; 

 
 
We can also do both of these steps simultaneously.
 
 

int b = 1; 

 
 
You can calculate with the number types "int"," "float", and "double".
 
Important: Although it works to save a number into a string, it can't be used for mathematical operations on it because it's text. It can be thought of as calculating with letters.
 
 
 

Advanced logical operators

I'll a*sume that you don't need to explain what the basic true/false logic is. However, if someone needs any help with this, you can leave a comment.
 
Let's now discuss advanced logical operation.
 
 
 

If-function

Let's get started with the "if_function". It compares 2 parameters. If the comparison is true/correct, it executes the included code. Exemples are a good way to explain it.
 
Let's imagine that we already have a rotating rotation rotor. If it is at 30 degrees, a lamp should switch on.
 
As in the previous script, we begin by saving our blocks into variables.
 
 

IMyMotorStator Rotor = GridTerminalSystem.GetBlockWithName("Example_Rotor") as IMyMotorStator; 
IMyInteriorLight Light = GridTerminalSystem.GetBlockWithName("Example_Light") as IMyInteriorLight; 

 
 
Let's get started with the "if function".
 
 

if (Rotor.Angle == 0.5236f) 

 
 
This is called the head. The argument can be written between the "()". This property requires that the angle is in radiant. 30 is the conversion of 0.5236rad into degrees.
 
 

{
Light.Enabled = true;
//This turns on the light, but more about that later.
}

 
 
This is the body. It contains code between the "".. It contains the code between and the function.
 
 
This function will initially check if "()” argument is true. If so, it will execute code between "". If not, it ignores the code between ""..
 
 
We can also upgrade' an "if-function" by using "else". This allows us have code executed in the wrong case. If the statement of the "iffunction" is true then the "else", will be ignored.
 
In this example, the light should turn off if its rotor is not at least 30 degrees.
 
 

if (Rotor.Angle == 0.5236f)
{
Light.Enabled = true;
}

else
{
Light.Enabled = false;
}

 
 
Finally, you can also add an 'if else'. It will act like an "else", but it also requires a condition to be true in order for it's code to work. Let's take for example that we want 4 lights of different colours, and that we want one light to be on for each quarter.
 
We first need to create variables in order to make our new lights.
 
 

IMyInteriorLight LightGreen = GridTerminalSystem.GetBlockWithName("Example_Light_1") as IMyInteriorLight;

IMyInteriorLight LightBlue = GridTerminalSystem.GetBlockWithName("Example_Light_2") as IMyInteriorLight;

IMyInteriorLight LightYellow = GridTerminalSystem.GetBlockWithName("Example_Light_3") as IMyInteriorLight;

IMyInteriorLight LightRed = GridTerminalSystem.GetBlockWithName("Example_Light_4") as IMyInteriorLight; 

 
 
Let's get to work building our "if/function"
 
 

if (Rotor.Angle >= 0 && Rotor.Angle < 1.571) 

 
 
This determines if the angle reaches 0 or more, but also if it falls below 90 degrees (1.571rad). Both conditions must exist for the statement of truth to be true. To make the statement true, you must be the "Or-operator", ("
 
 

{
LightGreen.Enabled = true;
LightBlue.Enabled = false;
LightYellow.Enabled = false;
LightRed.Enabled = false;
}

 
 
The green light switches on, all other lights are turned off.
 
We will continue this practice for the other areas.
 
 

else if (Rotor.Angle < 3.142) 

 
3.142 rad equals 180 degrees. It doesn't matter if it's higher than 90 degrees.
 
 

{
LightGreen.Enabled = false;
LightBlue.Enabled = true;
LightYellow.Enabled = false;
LightRed.Enabled = false;
}

else if (Rotor.Angle < 4.712) 

 
4.712 rad equals 270 degrees.
 
 

{
LightGreen.Enabled = false;
LightBlue.Enabled = false;
LightYellow.Enabled = true;
LightRed.Enabled = false;
}

else
{
LightGreen.Enabled = false;
LightBlue.Enabled = false;
LightYellow.Enabled = false;
LightRed.Enabled = true;
}

 
 
For the last case, we don't need an "elseif", because the only angles left to us are those that we want in this condition anyway (E270-360 degrees or).
 
 
 

Switch

The "switch", is the next function you can use. It's similar in function to an "if"/"if else" constellation, however all your conditions must be based only on one variable. It can't be compared with "==".. It wouldn't be possible to compare it with the previous example as we needed () for our conditions.
 
 
The following example shows how we changed it to reflect the conditions being an exact angle, and not an area. Let's take 4 cases. 0, 90°, 180° and 270°. Every angle will make one of our lights turn on and all the others off.
 
 

switch (Rotor.Angle) 

 
 
This is also the head. This is the head. This can also serve as a variable.
 
 

{
case 0: 

 
 
The angle is compared with the "0". This can be thought of as an "if" (Rotor.Angle == 0.
 
 

LightGreen.Enabled = true;
LightBlue.Enabled = false;
LightYellow.Enabled = false;
LightRed.Enabled = false;

break; 

 
 
The break will make it jump out of all the switches after the correct case is identified. This is important to save execution speed, as there won’t be any time wasted checking all the cases.
 
 

case 1.571:

LightGreen.Enabled = false;
LightBlue.Enabled = true;
LightYellow.Enabled = false;
LightRed.Enabled = false;

break;

case 3.142:

LightGreen.Enabled = false;
LightBlue.Enabled = false;
LightYellow.Enabled = true;
LightRed.Enabled = false;

break;

case 4.712:

LightGreen.Enabled = false;
LightBlue.Enabled = false;
LightYellow.Enabled = false;
LightRed.Enabled = true;

break;

default: 

 
 
This is optional. This case is used only if the other cases match the conditions. This case is used if the angle is between 0, 90 degrees, 180 degrees and 270°. For example, all lights can be turned off.
 
 

LightGreen.Enabled = false;
LightBlue.Enabled = false;
LightYellow.Enabled = false;
LightRed.Enabled = false;

break;
}

 
 
 

For-loop

For now, the "for loop", is the last. It is used when you want to repeat the same code for several variables. It is necessary to understand about the "list", in order to create those variables. It creates an object group, such as all light blocks in a grid. This is a slightly different process than creating a list.
 
 

List<IMyInteriorLight> Lights = new List<IMyInteriorLight>();

 
 
We have created the list "Lights". The list also needs a type. Therefore, a list can't include objects of a other type. In this instance, our type would be "IMyInteriorLight". This is the type space engineers use to make light blocks. But you can also create a list of numbers by using the type int.
 
Let's add all light blocks to this list:
 
 

GridTerminalSystem.GetBlockGroupWithName("Super mega station lights").GetBlocks(Lights); 

 
 
This includes all blocks in the group "Super mega-station lights" and the list at the end, "()")
 
"Lights[0]" is the phrase we use to address a single object from our list. Every object in our list has a number it can respond to. This number starts at "0", and goes up. If you create a 20-object list, the first object will answer "0" and the final one will answer "19". The number written between "[]" identifies the individual object. We would write it this way to turn on the third bulb:
 
 

Lights[2].Enabled = true;

 
 
The list is complete. Let's now move to the "for loop".
 
 

for (int i = 0; i < Lights.length; i++)

 
 
This is called the head. Between the (), we have three different parameters separated using a semicolon. () makes a variable called "i", with a value 0 and named "int". The third (parameter "i++") simply adds one to our variable, "i", each time the loop execution is completed. ('s second parameter "i Lights.length.length") sets the time that the loop should end. This means that the loop can be executed for as long a "i" value is lower than a particular value. If you wrote "i 20", the loop would be executed only 20 times. I used Lights.length to indicate the number objects in the "Lights." list. This basically means that "for-loops" are executed as often and as often as there are objects in the list.
 
 

{
Lights.Enabled = true;}[/code]

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.

 
 
 

Methods and properties

This could be one of your most difficult steps as a beginner. There are many different types of blocks in Space Engineers, which means that there are many different methods and properties. You'd have to ask someone to explain the difference between properties, methods, and I don't, but that doesn't really make it any less important.
 
The best way to work together is to read this
Guide – Github.com
 
While you are writing something. Malware deserves a huge credit for this. You will find all the block types and their methods as well as their properties (. That is basically what you could do with the block block). These are some of the most important things to know.
 
To use a property/method, first create a block variable. Next, add a '. then write your property/method.
 
 

Rotor.Attach();

 
 
Most Properties have the ability to set or get a value. However, some properties can only get values up to). You can give it a value by adding " = value", or not. These are two examples that will give you an understanding of how it works.
 
Getting a price:
 
 

float angle = Rotor.Angle;

 
 
This saves the angle to our variable "angle".
 
Setting a value
 
 

Rotor.RotorLock = true;

 
 
This unlocks the rotor lock.
 
You will also see the type of input that the property/method requires. This is important. The property RotorLock requires a "bool", which means it accepts only "true" and "false", as input or output.
 
 
 

Writing your first script

This will be visible if the programmable block is opened
 
 

public Program()
{
}

public void Save()
{
}

public void Main(string argument, UpdateType updateSource)
{
}

 
 
For now, we will ignore the "Program” & "Save" functions. Your entire code should be placed between the "" tags of the main function. This is where the script executes. This is what it would look like if you were to implement the first screen from the beginning.
 
 

public Program()
{
}

public void Save()
{
}

public void Main(string argument, UpdateType updateSource)
{
MyMotorStator Rotor;
Rotor = IMyTerminalGrid.GetBlockWithName ("Super mega station rotor") as IMyMotorStator;
Rotor.UpperLimitDeg = 45f;
Rotor.TargetVelocityRPM = 1f;
}

 
 
I strongly advise you to create your scripts outside Space Engineers. Visual Studio is the best combination of Malware's extension and Visual Studio. It offers many useful features that you will enjoy while coding. It also has a guide that explains how to install it.
Here – [github.com]
 
Malware's guide.
 
 
 

Cheat Sheet

Mathematical and Logical Operators:
 
"+"
 
"-"
 
"*"
 
"/"
 
"%"
 
"=="
 
"!="
 
">", ">=", "<", "<="
 
"&&"
 
"||"
 
"="
 
 
Value types
 
"bool": true/false
 
"string” in text
 
"int", number that does not include decimal places
 
"Float", number with decimal place
 
"double": larger number
 
"List" refers to a group of objects
 
"IMyInteriorLight", is an example of a blocked type
 
 
Advanced logical operations:
 
 

if (true)
{
}
else if (true)
{
}
else
{
}

switch (variable)
{
case value1:
break;

case value2:
break;

default:
break;
}

for (int i = 0; i < value; i++)
{
}

 
 
Properties/Methods:
 
 

block.method();
type variable = block.property;
block.property = value;

 
 
 

Epilogue

I will continue to develop this guide. If you have any suggestions, please let me know. This guide was designed for people who do not have any programming knowledge. Advanced programmers will need to consult other guides like
This one [github.com]
 
Malware.

 
 

Written by 33iRobot33

 
 
This is all we know about Space Engineers – How to program for beginners, this post original url can be found here and possible new updated to.
 


Be the first to comment

Leave a Reply

Your email address will not be published.


*