» Quick tips for Programmable Blocks

These are some quick tips for people trying to write scripts for Programmable Blocks in Space Engineers.

Persisting data

If you define variables outside of your main function, they will persist until the script is recompiled or the world reloaded.

For longer-term storage, IMyGridProgram has a Storage property. This is a string that is persisted and restored automatically by the game, and it lasts across compiles and reloads. All Programmable Block scripts extend from IMyGridProgram, so using it is as simple as reading and writing from the property:

void Main(string argument)
{
    // Read some persisted data from Storage
    Echo("Argument from last time was " + Storage);

    // Write some data to Storage
    Storage = argument;
}

Reading Solar Panel power

Screenshot of Space Engineers showing the control panel with a Solar Panel selected
Solar panel info in the control panel.

This seems to crop up quite often. While there’s no API to read the power output of a solar panel, you can read its detailed info field.  This is the text that shows up in the bottom right of the control panel. For solar panels it contains the max and current output.

So all you need to do to get the power in your script is find the block, pull its detailed info, and do some string manipulation. Hopefully we’ll get a proper API to read the power levels in the future, but this is a reasonable stop-gap until then.

void Main(string argument)
{
    var info = GridTerminalSystem.GetBlockWithName("Solar Panel").DetailedInfo;
    var lines = info.Split('\n');
    var output = lines[2].Split(':')[1].Trim(); // output = "110 kW"
}

Triggering actions

Way back in January, a very useful extension method was added to the API that dramatically simplifies how you call actions. It’s not mentioned in the official guide, though, and many people are still using the older headache-inducing method.

Instead of calling GetActionWithName and then having to Invoke it, you can simply call ApplyAction:

void Main(string argument)
{
    var block = GridTerminalSystem.GetBlockWithName("Beacon");

    // Verbose way:
    block.GetActionWithName("OnOff").Apply(block);

    // Better way:
    block.ApplyAction("OnOff");
}

Debugging scripts

Screenshot from Space Engineers showing an antenna with a custom name used for debugging
Antenna showing some custom debug information.

There are two easy ways to record debugging information from your script. The first is the Echo action, available from IMyGridProgram. This adds the text to the programmable block’s detailed info, and it will show up in the control panel when you view the block.

The other way is to use an antenna. Antennae names are displayed next to their markers in the UI, so anything you name an antenna will be visible to players within range of it. You can use this for debugging information, or even status information. Changing the name is a simple matter of calling SetCustomName:

void Main(string argument)
{
    var blocks = new List<IMyTerminalBlock>();
    GridTerminalSystem.GetBlocksOfType<IMyRadioAntenna>(blocks);
    blocks[0].SetCustomName("Received argument: " + argument);
}

Note that in this example I’m finding blocks by type, rather than by name. We’re changing the name in the script, so searching for it by name would be…. ill advised. If you need to deal with multiple antennae, consider putting a tag in the name (like [debug]) and searching for that. Just make sure when you change the name you preserve the tag.

comments powered by Disqus