-
Notifications
You must be signed in to change notification settings - Fork 123
The Anatomy of a Script
If you're not using MDK, you can skip to the next section
Ok, so you've learned the basics of C# and know where to get help, and you've opened your brand new project and you are looking at this (comments and using removed for brevity):
namespace IngameScript
{
partial class Program : MyGridProgram
{
public Program()
{
}
public void Save()
{
}
public void Main(string argument)
{
}
}
}
The first you need to be aware of, is that the namespace and the partial class Program : MyGridProgram
parts is not a part of the programmable block script. Only the parts inside the Program class is actually used when this script project is deployed to Space Engineers. The reason is that Space Engineers need strict control of what a script class is. A script class is always named Program
, and it's always inherited from MyGridProgram
.
This is what you'll see the first time you open a newly placed programmable block (comments removed for brevity):
public Program()
{
}
public void Save()
{
}
public void Main(string argument)
{
}
public Program()
{
}
It is used for one-time initialization of your script, and is run once every time your script is instantiated. This means it's run once after your game is loaded, or after you recompile your script. Recompiling happens when you have edited your script or when you press the Recompile
button.
The constructor is optional, you don't need it to have a working script.
public void Save()
{
}
The Save method is called whenever the game is saved, or right before you recompile your script. Here you can store any data you need to persist between play sessions. See The Storage String for more details on how to do this.
This save method is also optional.
public void Main(string argument)
{
}
or simply
public void Main()
{
}
The Main method is the main entry point of your script. It might be called many times during a script's lifetime, depending on what your script is and how it is set up. This method is required, obviously. The argument is passed from the game (depending on your toolbar setup) to your script, enabling a single script to do a bunch of different things. You can set this argument by dragging your programmable block onto a button panel, sensor or timer toolbar (or any other device toolbar except your suit) and selecting "Run". You will be presented with an input box where you can type in what you want to be passed to your script. It will be passed verbatim, Keep that in mind.
Do you have questions, comments, suggestions for improvements? Is there something I can do better? Did I make a mistake? Please add an issue here, and prefix your issue title with Wiki. Thank you, your help will be very appreciated!