Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

The Anatomy of a Script

Malware edited this page Jul 30, 2017 · 12 revisions

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:

The Constructor

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.

The Save Method

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.

The Main Method

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.

Clone this wiki locally