Managing Script Size & Complexity
Size matters. Your script has a hard limit of 100,000 characters
after minification. Malware whipped together an awesome tool for this, but nonetheless, keywords and language features are not minified and come at a cost. Second, never loop within a loop. Never. On a grid of any meaningful size, this could cause your script to crash. Mother Core uses a combination of techniques to operate regardless of grid size via the Block Catalogue.
Tips for Managing Size
You may need to compromise between what is the most safe/efficent, and what is least expensive in terms of character count.
Avoiding Multiple Block Type References
Throughout developing Mother OS, the largest character cost comes from accessing blocks by their type. Since these are whitelisted by the API, they cannot be minified and so come at great cost each time you reference them. For example, referencing a connector with IMyShipConnector
costs 16 characters.
Some notable block types:
Name | Character Count |
---|---|
IMyMechanicalConnectionBlock | 28 |
IMyBroadcastControllerBlock | 27 |
IMyProgrammableBlock | 20 |
IMyShipConnector | 16 |
Check out Malware's API Index for the entire list of available types.
Examples
Let's look at the common action of looping through a list of connectors:
List<IMyShipConnector> Connectors = new List<IMyShipConnector>()
foreach(IMyShipConnector connector in Connectors)
{
connector.Connect();
}
We've used IMyShipConnector
3 times and so will require at least 46 characters to complete this action. When you're doing some complex work, this only gets worse. Let's save some space with var
:
List<IMyShipConnector> Connectors = new List<IMyShipConnector>()
foreach(var connector in Connectors)
{
connector.Connect();
}
Not bad, we've saved 13 characters already! Since this is a simple action though, let's go even further:
List<IMyShipConnector> Connectors = new List<IMyShipConnector>()
Connectors.ForEach(connector => connector.Connect());
Boom - we no longer need any keywords within our loop and can save the brackets by putting the logic on a single line.
Being Conscious of Keywords
It is great practice to use keywords (private
, readonly
, etc.) as you are developing. However, keywords come at a cost as they are not minifiable, and so take up space each time you use them. Many of these are removed during the build process as this makes no difference to the computer once running in the programmable block.
Nonetheless, always be consicous of language level keywords and punctuation. 100,000 characters adds up fast.
For example, the programmable block will treat these two lines of code exactly the same.
Full keywords:
private readonly string SystemName = "Mother OS"
Partial keywords:
string SystemName = "Mother OS"
Important
It is good practive to ALWAYS use keywords. These exist for a reason and help keep our code clean and expressive. I suggest only reaching for these types of optimizations when all other cleanup has been exhausted.
Reducing String Size
There are generally two ways to print a string value in C#. You can do this via the ToString()
method on most objects, or via string interpolation where the string value of the provided object can be inferred. Let's look at each option.
ToString()
method:
float velocity = 123.45f
Mother.Print(velocity.ToString());
When minified, the string segment is 12 characters long:
Ò.ToString()
String interpolation:
float velocity = 123.45f
Mother.Print($"{velocity}");
When minified, the string segment is 6 characters long:
$"{Ò}"
Truthfully, I prefer the first method in most cases, but it comes at a higher cost. In the above example, using string interprolation results in a 50% reduction in characters - which is HUGE when you are printing a lot of data for the player.
Tips for Managing Complexity
If your script tries to conduct more than 50,000 operations in a single cycle, then you will hit a script too complex
error. This is almost always caused by the overuse of loops. You should do your best to use HashSets and Dictionaries rather than nested loops to control your logic. Check out Breadth-first Searching (BFS) and Time Complexity to get a better understand of this important computer science concept.
Tips
The Block Catalogue uses BFS to determine which blocks are on the current construct, as often a subgrid will have subgrids, which will have subgrids... you get the point. For those that have been using Mother OS for a while, this is likely the area that was causing you complexity errors before July 2025.