Mother Core (Script Framework)
Mother Core is a framework for developing custom Programmable Block scripts for Space Engineers. It enables you to build interoperable modules to suit the specific needs of your ship or station. Mother OS is built with Mother Core and a collection of extension modules.
Common Activities
Overview
Scripts built with Mother Core depend on the Program instance, and a collection of Core Modules. These core modules provide a wide range of functionality, including intergrid communication, event handling, and block management. Developers can add their own functionality via Extension Modules.
Recommended File Structure
A Module is the primary unit of functionality. Therefore modules should in most cases manage their own commands and events. This allows modules to be self-contained and reusable across different scripts. Mother Core and Mother OS observe the following file structure:
/
├── Program.cs
├── thumb.png
├── Modules/
├── MissileGuidanceModule/
├── MissileGuidanceModule.cs
├── Commands/
├── LaunchCommand.cs
├── DetonateCommand.cs
├── Events/
├── MissileLaunchedEvent.cs
├── MissileDetonatingEvent.cs
The Program Wrapper
We use the existing Program
wrapper created using MDK2, but with a few modifications that allow Mother to do her thing. We register our Modules in the constructor, and hook into the Save()
and Run()
methods to give Mother complete control.
partial class Program : MyGridProgram
{
private Mother mother;
// Program constructor
public Program()
{
// Create the Mother instance
mother = new Mother(this);
// Register Extension Modules
mother.RegisterModules(new List<IExtensionModule> {
new DoorModule(mother),
new LightModule(mother),
new MissileGuidanceModule(mother),
...
});
}
// Save before recompile or when triggered
public void Save() {
Storage = mother.Save();
}
// Run Mother each time the Program runs
public void Main(string argument, UpdateType updateType) {
mother.Run(argument, updateType);
}
}
The Extension Module
You can add new functionality to the program by creating an Extension Module. These modules are registered in the Program
constructor using the RegisterModules()
method. They may access all other modules directly and respond to changes when other modules emit Events.
We will use the MissileGuidanceModule
as our example. It provides functionality to launch and detonate missiles, and updates the thrusters on each program cycle.
class MissileGuidanceModule : BaseExtensionModule
{
// Boot the module
public void Boot()
{
// Reference important modules
FlightPlanningModule = Mother.GetModule<FlightPlanningModule>();
// Register custom terminal commands like 'detonate' and 'launch'
RegisterCommand(new LaunchCommand(this));
RegisterCommand(new DetonateCommand(this));
// Load relevant blocks from the grid
Thrusters = Mother.GetModule<BlockCatalogue>().GetBlocks<IMyThrust>();
// Listen for events
Subscribe<MissileLaunchedEvent>();
}
// Run module every program cycle
public override void Run()
{
UpdateThrusters()
}
// Do something...
void UpdateThrusters() {
Thrusters.ForEach(thruster => {
// ...
})
}
}
As you can see, connecting logic with Modules is easy and most of it is done in the Boot()
method.