Programming guide for beginners – Space Engineers

Programming guide for beginners – Space Engineers 1 - steamclue.com
Programming guide for beginners – Space Engineers 1 - steamclue.com

This guide is designed for people who are new to coding. It will help with your first scripts in Space Engineers. 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 also didn't learn programming/coding. My knowledge came from google research and annoying my friends with stupid questions about C#. This guide may contain minor errors or incorrect terms. However, I am certain that this guide will help anyone who doesn't have any scripting experience to create simple scripts in order to assist Space Engineers.
 
 
 

Example Script

I believe examples are the best method to learn Space Engineers scripting. We will move the rotor to a point in this first one.
 
We need to tell the game the block we want to address at the beginning. To make it easier to read, we will save this block into a variable. Let's make a variable for our rotor.
 
 

MyMotorStator Rotor; 

 
 
Every variable must have a type of ("MyMotorStator",), and a name (" Rotor,). The type determines what value can be saved to the variable. This value can be a number or text, but in our case it is a Rotor Block. This value is "MyMotorStator". You can use any name, but it's best to choose one that is easy-to-read and understands, especially as it gets more complicated.
 
Each line of code (should have a semicolon at the end. This is easy to forget, but it can cause scripts not to work as intended.
 
 
Now we will assign the variable its value: It is called the rotor block. In our case, the rotor blocks is named "Super mega station rotation".
 
 

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

 
 
The type is only mentioned when a variable has been created. You can simply give a variable value by using the "=" symbol.
 
This is the way to address the block within our grid. We simply place the block's exact name between the two "". We won't spend too much time on the text. We'll come back later. We will need to write the type again at the end in order to verify that the block that is being assigned to our variable has exactly the right type. If you forget this, and there is an LCD panel named "Super Mega Station Rotor", it would get assigned the variable. This may not sound necessary, but it's a safe script.
 
Now, whenever we use the variable Rotor in our script, the game addresses the rotor block.
 
 
Let's now move our rotor towards a particular point. We want it to rotate 45 degrees at a positive speed. There are two things we need to do. The first is setting the maximum angle of the rotor to 45 degrees. The second is to give the rotor its speed.
 
Let's start at step 1.
 
 

Rotor.UpperLimitDeg = 45f; 

 
 
We first tell the game we want the rotor blocks to be used. Next, we write a "." Next, we add the property or method that we want to use. This property must be valued to determine the maximum limit that the rotors can use. So we write "45". The "f", which is the type value (, in this instance a number. I will return to that) later.
 
 
Let's talk about the velocity setting. This works in a similar way, but the property is quite different. In this instance, we set the velocity to 1.
 
 

Rotor.TargetVelocityRPM = 1f; 

 
 
That's it. Once the script has been executed, the rotation will continue at 1rpm until the rotor reaches 45 degrees.
 
The entire 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 summarize it.
 
To make the script easier to read, we first save (s block) into variable. (This is how you could write it:
 
 

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

 
 
But I think it's much better to use variables.
 
Then, we can use properties or methods that allow us to change the settings for the blocks.
 
 
 

The basics of coding in Space Engineers

You don’t have to remember everything. I will add a chapter which lists everything you may need. I will attempt to explain the most basic aspects of nearly every script in this part.
 
 
"//" or "/* *//" mark comments within your script. These comments will be ignored if the script is executed. This means that you can explain your code or simply give a headline. "//" will work for the current line. "/*///" will enclose the comment. Commenting is important for understanding a script.
 
 
 

Mathematical as well as logical operators

Let's start by looking at simple mathematical and logic operators. This should be no problem.
 
I will just list the most crucial.
 
"+": Added
 
"-": Reduction
 
"*": Multiplication
 
"/": Department
 
"%":Modulo. This will return a remainder of a division. Examples: (5/2 = 2; remainder = 1- => 5/%2=1; 7/%4=3;
 
 
"==": Equals; used to compare values.
 
"!=": Different; basically the reverse of "==".
 
">", ">=", HTML4_). Also used as a way to compare values.
 
"&&": and all the variables that are related must match in your compartion
 
"
 
 
"=": In scripts, this is mainly used for assigning values.
 
 
 

Variable types

Let's continue to discuss variable types:
 
"bool", a type of variable that can only contain one of these two values: "True" or "false".
 
 
"string": This is text. A simple example would be to save text in a variable to display it later on an LCD monitor.
 
 
"int”: This is a number, but without any decimal points. The value won't be rounded. Instead, all decimal places are just removed. A 4.9 saved to "int" will be 4.
 
 
"Float” : This number can also be called a number but it does not have decimal places.
 
 
"double": A third type of number. It is similar in concept to "float", but can have a larger number. This won't be very important for basic programming. If you are using double as input, I would recommend using floating point. It is usually considered "double" when you assign a number to your script. To be treated "float", add an "f", such as "4.9f", after the number.
 
 
"IMyMotorStator",: This should be familiar. It is a block type made by Space Engineers. There are many, and we will ignore them all for now.
 
 
To initialize (we need to create) a Variable. We just need to write the type and name of the variable, and then end with a semicolon.
 
 

int a;

 
 
After this, it can be given a value.
 
 

a = 1; 

 
 
We can also perform these two steps simultaneously.
 
 

int b = 1; 

 
 
It is possible with the three types of numbers "int", floating, and double to calculate.
 
Important: Although you can save a number to a "string", mathematical operators cannot be used on it as it is seen as text. It can be pictured as calculating with letter.
 
 
 

Advanced logical operations

I will assume that you don't need to explain what the basic true/false logic systems does. For anyone who needs assistance, feel free to leave a comment.
 
Let's discuss the use more advanced logical operations.
 
 
 

If-function

Let's start with "if-function". It compares 2 parameters. If the comparison is true/correct, it executes the included code. I believe that examples are the best way to explain this.
 
Let's say that we have an existing rotating rotor. At 30 degrees, a light should go on.
 
As in the first script above, we start by saving our block into variables.
 
 

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

 
 
Let's start by looking at the "if-function".
 
 

if (Rotor.Angle == 0.5236f) 

 
 
This is the head. The argument is written between (). This property requires the angle in radiant. 30 degrees are converted from 0.5236 rad to degrees.
 
 

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

 
 
This is your body. It contains all the code between "".. It contains the code between "" and "".
 
 
This function will first check whether the argument in ()'s argument is true. Then it will execute the "" code if the argument has been correctly. If it doesn't, it will ignore all code between ""..
 
 
We can also "upgrade", the "if" function with an "else". This allows us to have code executed if the argument does not match. If the statement of the "iffunction" is true then the "else” will not be used.
 
In this example, we want the light not to turn on if the rotor temperature is below 30 degrees.
 
 

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

else
{
Light.Enabled = false;
}

 
 
Finally, you can add an "if elsewhere". It will act as an 'else' but it also has conditions that must be true for it to execute its code. Let's assume we want four lights with different colours. A specific light is to be turned off for each quarter.
 
First, we must create variables to 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 begin building our "if"-function.
 
 

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

 
 
This not only checks if the angle has been set at 0 or greater, but also whether it is below 90 degrees (1.571 rad). Both conditions must be met before the statement can be considered true. For the statement to be true, you must have one of the following conditions:
 
 

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

 
 
The green light turns on, and all other lights go out.
 
We will do this again for the other areas.
 
 

else if (Rotor.Angle < 3.142) 

 
3.142 rad equals 180 degrees. It doesn't matter whether it is above 90°. This "if other" will only check if the "if condition" is false, and the angle is above 90°.
 
 

{
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;
}

 
 
We don't have an "else" if in the last case, as the only angles we have are the ones we want for our condition anyway (.270-360 degrees.).
 
 
 

Change

The "switch", which is the next function we can use, is the second. It is very similar to a combination of "if" and 'if else", but you must only use one variable for your conditions. It can only be compared to a "==".. For our last example, we needed something smaller than (").
 
 
In the following example, the conditions are changed to be an exact angle rather than an area. Let's suppose we want to have 4 cases. 0, 90 degrees, 180 degrees and 270 degree angles. Every angle can turn one of the lights on and off.
 
 

switch (Rotor.Angle) 

 
 
This is called the head. The "()", the value that we want is written. This can also be an variable.
 
 

{
case 0: 

 
 
The angle can be 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; 

 
 
After the correct case is identified, the break will cause a script to jump from the whole switch. This is important in order to save execution time as there won't be any time spent checking all the other 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 when other cases do not match the condition. This is the case in our example. It is used when angle is between 0, 90 and 180 degrees. We can, for example, turn off all lights.
 
 

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

break;
}

 
 
 

For-loop

The "for" loop is the last. It is used in order to repeat the same code with a group variables. It is necessary to understand the "list" to create that group. It creates the group of objects you put in the list. It is slightly different than creating a variable when you create a list.
 
 

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

 
 
A list was created with the name Lights. The type of the list is also important. A list can't contain objects that are not of the same type. Our type is "IMyInteriorLight" in this case. This is the type light block in Space Engineers. You can also make a list with the type 'int'.
 
Let's fill this list with all the light blocks that are available on our grid.
 
 

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

 
 
This brings all blocks belonging to the group "Super megastation lights" into the list that is mentioned between the) at the bottom.
 
To address a single object on our list, we use the phrase "Lights[0]". Every object on the list has a number that it responds to. This number begins at "0" for each object and is counted up. If you have 20 objects, the first will respond to "0" while those below it will respond to 19 The number between "[]" refers to the object. This is how you would turn on the third bulb:
 
 

Lights[2].Enabled = true;

 
 
We have now created the list. Let's start the "for loop".
 
 

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

 
 
This is your head. We have 3 different parameters separated with a semicolon (()). () first creates a variable named 'i' with a value of 0; the second ('int i= 0'). The third parameter (E++) simply adds a "1" to our variable i every time the loop is executed. The second parameter (is "i Lights.length".) indicates when the loop should close. This means that the loop runs as long as "i' is lower than a given value. If you wrote "i 20", the loop would be executed only 20 times. In this example, I used Lights.length. This gives us the number objects in the "Lights." list. This basically means that the for-loop is executed as frequently as the 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.

 
 
 

Properties and methods

This may be one of the most difficult steps for a beginner. There are many types and methods for building Space Engineers blocks. If you want me to explain the differences between properties and methods, I would have to ask another person. However, that doesn't really matter.
 
The best way for them to work together is to read this.
guide – http://github.com
 
While you are writing something. Malware deserves enormous 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 just a few of the things you should know.
 
Your block variable must be written, then you should add a "" to make it a property/method. Next, add 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 either give it a number by adding something like "= value" or "no value". Here are two examples that will help you understand how it works.
 
Getting a value
 
 

float angle = Rotor.Angle;

 
 
This allows us to save the angle and make it a variable, "angle".
 
Setting a value
 
 

Rotor.RotorLock = true;

 
 
This unlocks the rotor lock.
 
The guide will also indicate which type of input a property/method requires. This is vital. This is vital.
 
 
 

Writing your first script

You can see the following:
 
 

public Program()
{
}

public void Save()
{
}

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

 
 
For now, we'll ignore the "Program” or "Save" functions. Your entire code is placed between the "" elements of the "Main” function. This is what gets executed when the script's code is used. This is how the first script would look if it were implemented 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 highly recommend that you do not use Space Engineers to write your scripts. Visual Studio combined with Malware's extension is my preferred method. It has many wonderful features that you'll love when coding. It also comes with a guide for how to install it and how you can use it.
Here – http://github.com
 
Malware's guide.
 
 
 

Cheat Sheet

Mathematical as well as logical operators
 
"+"
 
"-"
 
"*"
 
"/"
 
"%"
 
"=="
 
"!="
 
">", ">=", "<", "<="
 
"&&"
 
"||"
 
"="
 
 
Types of Value
 
"bool": true/false
 
"string" in text
 
"int", number without decimal points
 
"Float", number with decimal places
 
"double": larger number
 
"List" group of objects
 
"IMyInteriorLight", a block type example
 
 
Advanced logical operator:
 
 

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 intend to improve this guide. Please contact me if you have any ideas. This guide is meant for people who have no knowledge of programming. Other guides are recommended for programmers who have more advanced programming skills.
This one – [github.com]
 
Malware.

 
 

Written by 33iRobot33

 
 
This is all we can share for Programming guide for beginners – Space Engineers for today. I hope you enjoy the guide! If you have anything to add to this guide or we forget something please let us know via comment! We check each comment! Don’t forget to check SteamClue.com for MORE!
 


Be the first to comment

Leave a Reply

Your email address will not be published.


*