Space Engineers

Space Engineers

162 ratings
Introduction to In-Game Coding
By Jota
This guide is most intended for people who have no to very little undestanding of coding in C# or any other programming language. It teaches the basic of in-game methods and actions along with basic programming explanation.
4
   
Award
Favorite
Favorited
Unfavorite
Purpose and presentation
This guide is most intended for people who have no to very little undestanding of coding in C# or any other programming language. I'm personally not a programmer, but a computer enthusiast, and have gone through learning a series of programming languages. My largely use of it in a game was at Garry's Mod, creating a Gamemode using Lua.

What you'll learn
I'll present you a simple piece of code and explain the syntax, what it does and why. After that I assume you can adapt it and create your very own code. Please note that there are some things that can't be done at the present time (Update 01.063), but feel free to ask. I'll try to answer what is inside my knowledge.
Let's Start
This is the code:
List<IMyTerminalBlock> grid = new List<IMyTerminalBlock>(); void Main() { grid.Clear(); GridTerminalSystem.GetBlocksOfType<IMyInteriorLight>(grid); for(int k = 0; k < grid.Count; k++) { grid[k].SetCustomName("Interior Light " + (k + 1)); grid[k].RequestShowOnHUD(true); } }

First things first, introduction to proper coding
The first thing you might notice is the spacing. In programming, it's a common practice to use spacing to say which things are inside others. (e.g the grid[k]... block is inside the for thing, so I do some spacing to get it inside). This practice is called Indentation. It's not mandatory, but using it causes the code to become much more maintainable (and easy to learn).

Another thing to notice before we start to look at the code is the Main() method. You can think of a method, in programming, as a series of commands that are executed in the order they are written. There are lots of existent methods and you can create your own, but the Main() method is important among all other methods because it's the first to be called by the code. Imagine it like this:

I want to open Steam so I can play Space Engineers, but I don't turn on my computer and Steam automatically opens. First, Windows has to load, and then it loads Steam.

Windows is like the Main() method, it's called first and then calls all the other methods in the code. Also, why do I keep to write Main() instead of Main? Methods can have parameters or arguments which come inside those parentheses ( and ). Going back to the Windows, what if I want to start it in secure mode? I would call my "Windows" method as Windows(secure), what about normal mode? Windows(normal), and so on. Main does not have arguments so the parentheses are empty.

Finally, the braces { and } indicate where the method definition starts and end. You can see that most of my code is inside the Main() method, except the first line. But let's go for the next step.

Undestanding the code
The first line says the following:
List<IMyTerminalBlock> grid = new List<IMyTerminalBlock>();

What we're doing here is simply creating a variable, like x, in math. In this case, a list. The part left to the equal is telling the compiler (The supernatural entity that read our code and translate it to the computer) that we are creating a List of objects whose type is Terminal Blocks and its fancy name is grid. The name is arbitrary, but i called it grid. The code right to the equal properly creates the list. You'll almost always use this variable. It's our way to access all the blocks in our ship/station.

Next, we define our already know Main() method. All the code for now on will be called when we press "run" in the computer block. I would like to explain why the void thing, but you can search it for yourself, can't you? Just know it has to be there, for now.

Now, we call the Clear() method. Noticed there aren't brackets? That's because we aren't saying what clear has to do, we're simply telling it to do what it is already programmed to make, clear a list. In this case, we want it to clear our list, grid, so we write:

grid.Clear();

The semicolon tells the compiler that we ended this command call, so he can go to the next. And we too:

GridTerminalSystem.GetBlocksOfType<IMyInteriorLight>(grid);

Now that our list is clean, we can rewrite it with whatever we want. With the above line, I asked the GridTerminalSystem , which is an already defined variable in -game to look for all the Interior Lights in the ship/stationa and put them in our grid list. IMyInteriorLight is the type we use to look for interior lights, there are others, like IMyRefinery, IMyBeacon and IMyRadioAntenna. You can find them all in the link I provided for the GridTerminalSystem.

Once they are in the list we can access all of them using a loop, in this case, a for loop. Imagine our list now is something like that:

[Light 1][Light 2][Light 3]...

Every [ ] represents a slot of your list which has an index, we can access each slot using its index. The first one has index 0, the second 1, the third 2, and so on.

for(int k = 0; k < grid.Count; k++)

To help us remember in which slot we stop, we create another variable, k, which is an integer, from which comes the int type.
k++ makes the value of k increases by 1 from 0 until it is less than (<) the size of our list grid. Remember the Clear() method, this time we aren't calling a method, but we're getting the the size of a list, which is Count to our specific list, grid.

{ and }, as in the method, say where the loop starts and where it ends.

grid[k].SetCustomName("Interior Light " + (k + 1));

and

grid[k].RequestShowOnHUD(true);

are calling two methods. First, remember that I said we can acess a list slot using its index? we do it like that:

listNameGoes[slotIndexGoesHere]

So, our list name is grid, and the slot we want to access is k. This make sure that we call the method to all the lights in the list, not just one.

Now that we have the objects to cast the methods, we can do so:

SetCustomName("Interior Light " + (k + 1));

SetCustomName() is a method to change an object name, and its only argument is the name we want it to be. Interior Light plus the index of the light. As our indexes start at 0, and we don't want a "Interior Light 0" we do (k + 1). The first + signal is concatenating the text "Interior Light ", which is a string and must be written inside quotation marks "" with the number that results from k plus 1. Again, the semicolon ends the command.

grid[k].RequestShowOnHUD(true);

If you undestood the last line this one is a lot easier. We call the RequestShowOnHUD() method, which is equal to press the "Show on HUD" button in-game for each interior light in the list. This time, the method argument is a boolean or bool which means its either true or false. As we want it to show up, we write true.

The last bracket ends the Main() method and with it, our code.
Conclusion
I hope this guide helped you undestand not only about Space Engineers coding, but programming at all. But, the question remains, what do this code do? The answer is below, but if you undestood everything, I think you can bet. If not, have a look at the answer and see if it helps.
Answer
It gets all the Interior Lights in you Ship/Station and rename them in crescent order. Then, it shows them all on the HUD.
23 Comments
Altonoj 26 Nov, 2017 @ 12:58pm 
Hi Jose, hope you are monitoring this guide. Have a question, I get an error message "system. void cannot be used from C#--use typeof(Void) to get the void type object" I have tryed several thing that didn't work. Help!!!
clonecommando6 26 Sep, 2016 @ 2:53pm 
ok is it possible to see if a sensor is giving off a positive input and if it is to do something and if it isnt then do something different and make it repeat?
Wackaboom 12 Jan, 2016 @ 6:45am 
Thurow and good explanation, thanks for taking time to do this!
zooxanthellaeh2o 16 Oct, 2015 @ 9:13am 
this is great help. thank you for taking the time to write it.
DerFedus 5 May, 2015 @ 7:22am 
Wow... 10 years since left studying TurboPascal 7. And now some coding makes me dizzy.
This guide is quite a big help for those starters over SE programming school.
Thanks a lot!
Jota  [author] 2 May, 2015 @ 5:56pm 
You can use the Substring method described here:
https://msdn.microsoft.com/en-us/library/ms228362.aspx
LioStar 1 May, 2015 @ 9:02pm 
Hi, very good guide, it helped me. But i need some more help, i dont know very well C#. I want to format a string ("000" + integer) and get the 2 last characters. Any help ? Thanks :)
BluePhoenix8406 13 Apr, 2015 @ 4:58am 
Is there a complete list of commands with examples. Its been a while since i have used C# iam learning Visual Basics in school.
Mr Beans 13 Mar, 2015 @ 5:28am 
wow that was really good and clear.
Jota  [author] 4 Feb, 2015 @ 7:20am 
imo grouping blocks so they load as one single piece of geometry won't going to make the game smoother. What matters for the performance is the triangle count, grouping the blocks would not reduce it and, as we're talking about a big piece, it would take more time to load than normal.