Programming Tricks

After mastering the Programming Basics and all of the Programming commands and blocks, there are still improvements to be made to your bots' behavior. Depending on the situation, you might want to make your programs smaller or make your bots do their jobs faster. Below are some tricks you can use to optimize for these and other factors.

Memory
Optimizing to reduce memory usage, or the number of commands and blocks in your program, can be important when you want fewer bots to do more jobs, or when you want a bot to be more robust in dealing with failure modes.

Loop Commands
Some commands have no effect in certain situations. This can be used to optimize almost-identical blocks of code into loops.

Consider a bot carrying three different items, one in hand and two in the backpack, all of which need to go into a workbench. This trick also works with non-loop instructions if you turn on 'exit on failure' in the loop, but beware other unexpected failure modes.

Eliminate Unnecessary Moves
A bot can perform some actions while adjacent to the target of the action, without moving. If you arrange your buildings and operations with this in mind, you can omit a move from your program. An early-game example of this involves the Chopping Block and Crude Workbench. Because the input and output spaces are adjacent, a bot does not need to move to the output space before picking up whatever they just made. Consider this recorded partial program for chopping a log into planks then picking up one of the planks:

Speed
Every command takes some time, so reducing how often your bot runs unnecessary commands can make it perform much faster.

Don't Repeat Moves
Moving to a place where your bot already is takes time with no effect.

Try Until Failure
Bots often perform a task that can sometimes be performed multiple times in the same place with the same target and sometimes not. Fortunately, attempting the task again will usually just fail without unwanted side effects, and this can be used to speed up operations. Consider a bot delivering resources to blueprints under construction. The second bot will keep trying to shove items into the blueprint until it won't take any more. When it runs out of items, the blueprint is full of that item, or the blueprint is built, the Add will fail and the inner loop will exit. The second bot will add items much more quickly without having to re-run the Find and Move for each item it adds to the same blueprint. This will also avoid other bots "reserving" the blueprint.