Space Engineers

Space Engineers

103 ratings
Programmable Block - Hello, Galaxy! (Beacon)
By Textor
A quick "Hello, World" type guide. This is for beginners to C# who want to use the programming block-- and would like to have everything broken down, step-by-step.
   
Award
Favorite
Favorited
Unfavorite
Hello, Galaxy!
Hey All,

Here is a quick "Hello, World" script. Bolded words are found in the glossary at the bottom.

For the new programmers out there, the traditional first script you'll make is a "Hello, World" script, since it is very simple and lets you start getting used to the syntax of things.

So, first, what you will need:

1. I recommend Creative Mode for this-- you're learning, why bother messing with all that resource gathering?
2. Create a station platform (really, you can make ANY type of ship you want, but this is simple... no motion or drifting to worry about).
3. You'll need the following components present: Beacon, Power Source, Programmable Block, Button Panel

Set up the button panel to point to the programmable block with action "Run"
Set up the beacon to be named "Beacon" (if it isn't already).

Ok, now to the actual programming bit:

Go to the programmable block and click "edit"

You'll notice that Keen has been kind enough to fill out your void Main() method already. void Main() is the entry point to the code.

Go to line 2 (the '{' ) and press "enter" to get a new line. You should now be on a blank line between { and }. Your code should now look like:

void Main() { }

Press TAB once. you should now be indented. Indentation is very important for readability. It isn't strictly necessary-- the code will execute without it, but it makes it a lot easier to follow what is going on when you properly indent.

You'll want to put in the following line:

IMyBeacon beacon = GridTerminalSystem.GetBlockWithName("Beacon") as IMyBeacon; //Variable definition.

So what does this line do?

IMyBeacon beacon
This tells the system that you want to define a variable named "Beacon" of type IMyBeacon. You may see other people define this as "var" but I feel that for readability's sake, IMyBeacon is a bit more appropriate -- you know exactly what you intend the variable to be right from the start.

= GridTerminalSystem
This is a Keen-defined class. It lets you access blocks on the same "grid" (same ship/station) as the programmable block.

GridTerminalSystem.GetBlockWithName("Beacon")
The '.' indicates that you are accessing a method or property contained within a class. In this case, you want to access the "GetBlockWithName" method that is part of "GridTerminalSystem".

GetBlockWithName takes a parameter. This parameter is a string. So we put "Beacon" into the parentheses so the method knows what to do. Quotation marks tell the system that everything within the quotation marks are a string. You cannot use GetBlockWithName(beacon) because that would pass the IMyBeacon variable into the method, which would case an error (IMyBeacon is not a string type.)

as IMyBeacon;

GetBlockWithName returns a block that isn't a beacon. This might be a bit confusing, but it goes back to the types used by variables. "as" tells the code to cast the variable type into another type of variable. In this case, it is telling the system to convert the "IMyTerminalBlock" given to us to a "IMyBeacon"

The semicolon (';') tells the system that you are done here -- end the instruction, and move on to the next instruction.

But wait, there's one more piece to that line!

//variable definition.
This doesn't have a semicolon, and what are those weird slashes in front!?

This is a comment. Comments in code like this aren't strictly necessary, but it is a good habit to get into. Comments let you as a programmer tell other people reading the code (or even a reminder to yourself) what something is doing. This is telling us that the line we just had was a variable definition. Comments are defined by two slashes in front OR blocked using /* and */.

// only works on ONE LINE. It works from the start of the two slashes to the end of the line.

/* */ only works BETWEEN THEMSELVES. You can use this to comment out part of a line, or ENTIRE BLOCKS OF CODE. Why is this important? You may hear people tell you to "comment out..." something. This lets you comment out large amounts of code (make it ignored by the system) for debugging purposes or to set it aside for later-- it isn't finished being worked on, it breaks the ability for the script to work, and you want things to keep operating while you work on this section of the code.

Finally, we get to the last line. You'll notice that in my code, I put an extra line between the two. This isn't necessary, just makes it a little more readable (I like to separate actions I take from variable definitions.)

Add the following line to your code, on a new line (either just below the beacon definition or spaced out with a blank line or two.) Don't forget to use TAB to indent!

beacon.SetCustomName("Hello, Galaxy!");//sets the beacon's name to Hello, Galaxy!

So, this is pretty easy to figure out:

.SetCustomName("Hello, Galaxy!");

You have a IMyBeacon variable named beacon. This beacon has a pre-defined method called "SetCustomName" which takes a single parameter: a name. So, we tell the system to set it to "Hello, Galaxy!" (without the quotes, of course) for us.

So, click "Check Code" down at the bottom. This is very important to always do. Why? Because it tells you if there are any problems. Did you forget a semicolon somewhere? Are you doing something that just isn't allowed? The check code button tells you! If you simply save and exit, the code may not work at all, and you'll be wondering why.

Go ahead and "Remember and Exit" your code.

And that's the code. So how do we run it?

Well, there are two ways that we've set up. Let's do it through button-- press that button I asked you to set up earlier. What happened to the beacon? It now says, "Hello, Galaxy!"

Pressing the button additional times won't do anything, since the beacon doesn't have the name "beacon" anymore. You can rename the original beacon, or you can build another beacon and name it "beacon" to do it again.

The other way to execute the code is to get into the control panel and press "run" beneath "edit" on the programmable block. But that's not as fun as pressing a button with your engineer, is it?

Full Code:

void Main() { IMyBeacon beacon = GridTerminalSystem.GetBlockWithName("Beacon") as IMyBeacon;//variable definition. beacon.SetCustomName("Hello, Galaxy!");//sets name to "Hello, Galaxy!" }
Glossary
  • void Main() - This is required in every C# program, and the internal block scripting is no exception.
  • Method - A set of instructions that can be reused in your code so that you don't have to keep typing it in over and over again.
  • Entry Point - This is where the script begins executing. Why is that important? Because you can have things before void Main(). The script doesn't execute from line 1 down, it executes starting at void Main(). void Main() will always be the entry point.
  • Variable - A word that contains data. This word is "typed" which means that it contains a specific type of data. It is always defined as: type variablename = value; Variables are case sensitive and can contain mixed case. Variables can contain numbers, but cannot start with a number. You should always name your variables descriptively. I could name a variable 'a', but that doesn't mean as much as naming a variable 'beacon' which doesn't mean as much as naming a variable 'imbBeaconToBeRenamed'. Use a naming scheme you like, but just remember that you and other people likely need to be able to figure out what things do if variables are too vague, which can cause headaches later.
  • Parameter - A variable that is passed into a method. This will be expanded upon more when I do a tutorial demonstrating methods.
  • String - A variable type that contains text data. "Hello, Galaxy!" is a string. "This is a string." is also a string. "This is not a string." is also a string (and a liar, to boot.) So is "asdfkasdjladafljkadlfjasdfhjasdhflsdajflja" though I don't know when you'd actually want to set a string to that.
  • Cast - This term refers to converting a variable from one type to another. For now, know that it is necessary to do this, but I'll have another tutorial to explain casting at much greater length.
  • ; (semicolon) - This tells the system that you are done with a line of code. Useful, in that you can break up long lines into multiple lines without having to do anything special, as long as it is terminated with a semicolon.
  • instruction - A single action that a system is told to do. int i = 0; is instructing the system to make a variable named i that contains whole numbers, and set its value to '0'. beacon.SetCustomName("Hello, Galaxy!"); is instructing the system to set a beacon's name to "Hello, Galaxy!"
  • Comment - As explained above, a comment is a section of the code that is ignored by the system. You do not have to end it with a semicolon, you do not have to follow any syntax conventions, you can write a novel in this area. The system skips right over it, as if it is invisible. The comments aren't for the system. Comments are for you. Use them! Remind yourself and anyone looking at your code of your glorious genius solution to something! Good documentation makes life easier, so comment to your heart's content!
22 Comments
hunterakagman 25 Sep, 2018 @ 5:15pm 
Can you update this? They changed some commands.
Altonoj 24 Nov, 2017 @ 11:41am 
Thanks BeatU4Klondike, For your Post. It doen't appear Textor is keeping tabs on this one. :)
Leo 7 Apr, 2017 @ 8:28pm 
I had a "Well duh!" moment while I was reading:

http://spaceengineers.io/spaceengineers/doc/interfaceSandbox_1_1ModAPI_1_1Ingame_1_1IMyTerminalBlock.html ("Properties" Section for the IMyTerminalBlock documentation.)

In python (which I'm very familiar with...) this would be called a "Data Member" element of the class. Most high level programming languages are similar in some form or another. Once I realized that CustomName was a Propertie/DataMember, It was easy peasy.
Leo 7 Apr, 2017 @ 8:28pm 
@highflyers and @BadgerBadger Was still digging my self on this since my last post due to my curiosity. It's obvious that I'm a bit new to C# as any experienced C# dev would be able to tell from the mere fact of question that was asked. (Programming over all is not new for me though.)
I did find the solution. It was really simple actually. It came after a few days of messing around with C# to get a better feel for the language. As well as digging in documentation.

Basically replace the line:
beacon.SetCustomName("Hello, Galaxy!");//sets name to "Hello, Galaxy!"
<with>
beacon.CustomName = "Hello, Galaxy!";//sets name to "Hello, Galaxy!"

Thought process for the find:
- IMyBeacon is a child class of IMyTerminalBlock (If I understood things correctly.)
- IMyBeacon would inherit elements from IMyTerminalBlock
- SetCustomName is a method of IMyTerminalBlock/IMyBeacon (now a obsolete method)
- CustomName is a property of IMyTerminalBlock/IMyBeacon (not a method at alll)
Highkicker11 7 Apr, 2017 @ 6:06am 
@textor, Mate i get the code and it works. But like beatu4klondkine and badgerbadget i would like to know what the correct code is now, so that we dont get the warning that de code is obsolete.
because its madening to have code work but the program telling you its wrong and not being able to find the information to fix the problem. so please please respond with a fix or point us to the place to find the information. because i have googled the crap out of it but cant find anything
Leo 29 Mar, 2017 @ 12:51pm 
@Textor: BadgerBadger is right. I'm a bit new to C# and programming in SpaceEngineers. I'm getting the same error he is getting. Code still works when the code is ran. But I would like to follow current practices for the API. What would be the proper current code to change the name of the beacon block?
BadgerBadger 20 Feb, 2017 @ 4:25pm 
I am getting the error:
'IMYTerminalBlock.SetCustomName(String) is obsolete:'
'Use the setter of Customname'

How do we set the name now with current versions of SE?
BeastUnchaind 28 Dec, 2016 @ 3:25pm 
Does the programmable block recognize keyboard commands? For instance I'm makeng a rover, and I want functioning brake and reverse lights. Could I make a script where the brake lights turn on when space bar is held or reverse lights turn on when "s" is pressed?
Sir_Mason 23 May, 2016 @ 8:19am 
Is there a list of the blocks name and the syntax for using them?
Myrkur 15 Dec, 2015 @ 2:01pm 
Question i jsut took over a drone a advisory drone and its owned by me and i has programmale blocks and two timers one timer is set to timer follow and i want it to follow me anyone know how? i can send you the code over steam message its over 1000 characters