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

Utility Class or Extension Class

Malware edited this page Sep 9, 2017 · 1 revision

Utility classes (normal classes)

Now you have created your project, you can either write your script in the Program.cs file as you're used to, or you can utilize the extra little tricks MDK has in store for you. You can use separate .cs files for your classes, making for a project that's a lot easier to manage.

You must realize, however, that you can't just add any old class and have it work as expected. As explained here, programmable block scripts are wrapped into a container class by the game. Visual Studio obviously does not have this wrapping built-in, so you're gonna have to add something to simulate that yourself.

You do this by wrapping your class like this:

namespace IngameScript
{
    partial class Program
    {
        public class ThisIsMyClass
        {
        }
    }
}

This is highly recommended in order to have your script work as you expect when writing the code. MDK provides you with a class template to help you create this wrapper. Right click on your project, select Add, then New Item.

Add New item Select Utility Template

Extension Classes

There is a significant difference between an extension class and a utility class. For the absolute majority of your uses, you're not going to need this - however, if you do want it, you can. Extension classes are a special construct which actually uses an officially sanctioned exploit to function. The "exploit" is left open because it's perfectly harmless. This is how to use extension classes directly in the programmable block:

public void Main()
{
    // Some cool code...
}

} // Forcefully close the wrapper Program class that SE uses around your script

public static class ExtensionClass
{
    // Extension code...
// } Deliberately remove the closing brace of the last extension class, because Space Engineers will add it back in

Of course, MDK does all that work for you. Any class not having the partial class Program signature explained above will be designated an extension class and added in the manner above automatically. No hacking around needed.

Clone this wiki locally