-
Notifications
You must be signed in to change notification settings - Fork 123
The Anatomy of a Script
Ok, so you've learned the basics of C# and know where to get help, and you've opened your brand new project and 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
. So the important part of this code file, then, is this:
public Program()
{
}
public void Save()
{
}
public void Main(string argument)
{
}
The rest of the code should be left as it is. Let me then explain the individual parts:
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. Here you can store any data you need to persist between play sessions. This is a bit out of scope right now, but it's nice to know.
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. This is beyond the scope of this tutorial.
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!