A guide to “coding” with combinators

This board is to show, discuss and archive useful combinator- and logic-creations.
Smart triggering, counters and sensors, useful circuitry, switching as an art :), computers.
Please provide if possible always a blueprint of your creation.
mechatimer
Manual Inserter
Manual Inserter
Posts: 4
Joined: Sun Nov 10, 2024 12:53 am
Contact:

A guide to “coding” with combinators

Post by mechatimer »

(This guide assumes you already know the basics of combinators and are comfortable with some simple programming terminology.)

For most players, building solutions for common problems—balancing oil, toggling between solar and coal with an RS latch, or setting train limits based on inventory, is pretty straightforward. And honestly, that’s already more than enough for a functional factory.

But then there are those of us who want more: Building computers, state machines, automalls, and other increasingly complex systems.

When I first set out to design a serious automall, I ran into a wall. My builds quickly became cluttered with one-shots, 1-tick delays, input insulators, and other timing hacks. I was spending more effort managing signals than actually implementing logic. It wasn’t scalable, and it definitely wasn’t clean.

What I realized was that I didn’t just need a solution. I needed a system. Something structured that could break problems into clear, sequential steps, manage complexity, and make designs easier to reason about.

After digging through existing approaches and experimenting with my own ideas, I eventually landed on a flexible method that worked for me.

This guide is an attempt to explain that system.

The Core

At the heart of this system are four combinators:
  1. A constant combinator for global variables — the Variables Combinator (VC). Toggling this combinator also acts as a full system reset/on-off switch.
  2. A constant combinator for global constants — the Constants Combinator (CC).
  3. A memory cell that tracks all declared variables and ignores all other signals.
  4. An arithmetic combinator that multiplies all signals by -1.
Core_Combinators.png
Core_Combinators.png (94.41 KiB) Viewed 92 times


The Data Bus

The system uses two wires:

* Green wire — carries all global variables and constants. Undeclared signals may also exist here, as long as they don’t interfere.
* Red wire — read-only, containing the negative of everything on the green wire. This is produced by the fourth core combinator.

Variables

To declare a variable, assign it a value of 1 in the VC. Once declared, any instance of that signal on the data bus will be tracked by the memory cell.

To change a variable, output the difference between the desired value and the current value onto the green wire for one tick. (More on this shortly.)

Constants

Any signal defined in the CC is continuously present on the data bus.

Timers

Any signal declared in both the VC and CC becomes a timer.

The value in the CC determines how much the signal increments (or decrements) each tick.
Since timers are also variables, they can still be written to directly.

The Instruction Pointer (IP)

The instruction pointer is simply a timer that increments once per tick. It controls sequencing and timing across the system.

I use the S signal as the instruction pointer.

On/Off Behavior

The entire system is designed to reset when the VC is turned off.
Turning it back on restarts everything from a clean state—similar to rebooting a computer.

Manipulating Variables

All variable updates follow a single pattern:

X = X + (something)
or shorthand: X += (something)

This reflects how Factorio’s memory cells work. A stored value is continuously fed back into itself. Any additional signal is added on top of that existing value. For example, if the memory cell holds X = 5, and you input X = 1, the result becomes X = 6.

Examples
X = 45 → X += 45 - X
X = 0 → X += -X
X = Y → X += Y - X
X++ → X += 1

The right-hand side of the += expression is what you output onto the green wire.

This is where the red wire becomes extremely useful: since it already contains -X, you can easily cancel out a variable’s current value while applying a new one.


Incrementing, decrementing, or resetting variables can be done with a single combinator.
Multiple independent operations can often share the same combinator.
Assigning a literal value (e.g. X = 45) is also straightforward with one combinator.

Example Image: When S == 5, Set X to 45, increment Y by 1
Setting_Literal.png
Setting_Literal.png (173.22 KiB) Viewed 92 times


More complex assignments (e.g. X = Y) typically require two steps:
Use an arithmetic combinator to compute the difference (Y - X → X)
Use a decider combinator to output the result at the correct time, usually gated by the instruction pointer

Example Image: When S ==6, set Z equal to X
Setting_Variable.png
Setting_Variable.png (265.31 KiB) Viewed 92 times
How to Use This Guide

From here on, this guide shifts from explanation to demonstration. The above explanation, paired with a few "code samples", should
provide a better understanding on how you could leverage this system for your own creations.
Below is a blueprint containing several examples (nested for loop, Fibonacci sequence generator, prime number generator, and more). Most combinators include descriptions.

I recommend exploring it in a sandbox map with god mode, all technologies unlocked, and the editor enabled. Using the editor’s time controls to pause and step forward tick-by-tick makes it much easier to follow what’s happening.

Tertius
Smart Inserter
Smart Inserter
Posts: 1619
Joined: Fri Mar 19, 2021 5:58 pm
Contact:

Re: A guide to “coding” with combinators

Post by Tertius »

Thank you for giving an insight how you approach a complex combinator task. It's a rare post, very seldom people talk about how they approach a combinator task and try to explain this to others in an extensive and comprehensive way.

I saw your automall but was a bit disappointed you didn't explain how it works in general, because that's the most interesting part (for me). It was one of these posts who just throws an image and a blueprint with no comment. Eat or die. However when I looked into the blueprint, I saw a well designed setup, not some incomprehensible mess of combinators. You managed to unroll the recursion with just a bunch of combinators and without an extra machine to just read recipes. You include a blacklist of items not to craft, an essential part. Impressive. It's nice you gave more information about the design in general now.

But the framework you presented isn't generic. Tasks are too different to always start with such a framework. It's difficult to judge when to use a framework like this, or when to do a custom design. It often happens that squeezing things into a framework is more work than the task itself. It could happen you mislead people with this framework if they don't thoroughly understand the circuit network in general and their task at hand.

Having said that, you mentioned 5 generic and universal concepts I'd like to point out, because it's valuable:
  • use constant combinators to configure parameters.
  • Multiple independent operations can often share the same combinator.
  • multiply a set of signals by -1 to enable building differences without more arithmetic combinators.
  • a counter (clock) can help organizing a workflow to initiate actions at specific points in time.
  • use the map editor to advance tick per tick to develop and debug a circuit network setup.
Post Reply

Return to “Combinator Creations”