Space Engineers

Space Engineers

Not enough ratings
Vanilla logic gate tutorial - no scripts
By Wojak
This tutorial will introduce You to logic gates in vanilla Space Engineers, no mods or scripts required, everything is based on the Event Controller block. The guide has been updated after the Warfare Evolution update to include important changes in that update.
The concept originated during the Automaton beta, You may view the original forum post if you want: https://steamcommunity.com/app/244850/discussions/0/3775742652418931712/
   
Award
Favorite
Favorited
Unfavorite
Video Tutorial
This is the original video:
This is a supplement after the Warfare Evolution update:
How dos it work – analogy to Minecraft Redstone
Like in any game that simulates electronic circuits You need something that is capable of boolean logic and is functionally complete + something that is able to transmit information between the logic blocks.
In Minecraft we have the redstone torch, which is a functionally complete NOR (NOT OR) gate, we also have the redstone dust that serves as wires, with that You can build any circuit You want.
In Space Engineers we have the Event Controller which, among other things, can be configured to work as the NAND (NOT AND) gate (also functionally complete), but is more complex then that, a single Event Controller can also serve as a OR gate or a standalone NOT gate. The concept of transmuting information between Event Controllers will be explained in the next chapter.
The difference between redston and Event Controllers is rather significant, redston is more intuitive, you can clearly see what parts of the circuit are connected, while Event Controllers are abstract, they interact witch other blocks trough the monitored block selection (inputs) and toolbar actions (outputs), but this also makes the circuits more compact. Event Controller is a complex block that can be configured to serve as any logic gate instead of XOR and XNOR.
The memory/state blocks – transmuting information between Event Controllers
One of the first limitations discovered was that the Event Controller can only handle one type of event, if you want to compere “Cargo Filled Percentage” with “Stored Power Percentage” you will need some kind of a mediator event and convert your monitored states.
A state holding block (memory block) should be capable of holding a clear boolean TRUE/FALSE state.
My first idea was to use pistons and their “Piston Position Percentage”, treating the retracted piston as the FALSE state and the extended piston as the TRUE state. On paper this was perfect, we got the set true action (extend), set false action (retract) and flip action (reverse), the “Piston Position Percentage” also has the option to select “is equal or greater then” (if TRUE) and “is less then” (if FALSE). The disadvantages however outweighed the benefits, pistons are 2x1 in size, the piston head is a subgrid, thee need to be even more space allocated for the piston extension, Lord Clang dos not always like them and they are expensive compered to other alternatives.
The second idea was to use “Block On/Off Switched” event and the cheapest switchable block – the interior light. We retain most of the features – On state is TRUE, Off state is FALSE, we got set On (set TRUE), set Off (set FALSE) and toggle On/Off (flip state). However we lose one important feature – “if FALSE”, this means that in some ceases anti memory blocks may be needed (On state indicates FALSE, Off state indicates TRUE), other then that the disadvantage is having only one attachment point (it is hard to cover the lights in armor blocks).

The block I ended up settling on is the Timer Block witch the “Block On/Off Switched” event, while being a bit more expensive, they have 6 attachment points and a clear indicator light on each side to show the On/Off state, all other properties are the same as for the interior light. While You can use any block witch the On/Off state, the Timer Block is my personal recommendation.
The Logic Gate basics – the inner workings of the Event Controller Block
Before we are able to preform any logic operation we need to concert every thing that interest us into the On/Off state of memory Timer Blocks, continuing the example lets say that we want to trigger an action if the “Cargo Filled Percentage” is less then 1% and “Stored Power Percentage” is more then 90%, the conversion process will require 1 Event Controller and 1 Timer for every type of event, in this case two of both. Giving specific names to blocks will help to keep track of the circuit structure.
In the “Cargo Converter EC” select the “Cargo Filled Percentage” event, then pick the “is less then” condition and set the threshold to “1”, add a monitored cargo block, then setup the actions, the first action slot is the “if TRUE” slot, so we use it to turn the “Cargo State TB” On, the second action slot is the “if FALSE” slot, we use it to set the “Cargo State TB” Off.
In the “Power Converter EC” select the “Stored Power Percentage” event, then pick the “is equal or greater then” condition and set the threshold to 90, add the monitored battery, in the first action slot set the “Power State TB” On, in the second action slot set the “Power State TB” Off.
Now that we got the “State blocks” we can setup the logic, in this case we need the AND gate, we place a new Event Controller, name it “Logic AND EC”, set the event to “Block On/Off Switched” , add the both state blocks to monitored blocks, tick the AND gate checkbox, in the first action slot trigger actions you want to happen wen the condition is TRUE, in the second slot trigger actions for the FALSE state.

- The state blocks can also be turned On/Off by sensors, buttons, timers or Logic Event Controllers,
- You can ad more then two state blocks to the monitored blocks section,
- Event Controllers have 9 action toolbars (9 actions for TRUE slot and 9 actions for FALSE slot),
- You can’t directly change the On/Off state of a block that is monitored by the same Event Controller, in this case the action slots in the Event Controller should trigger the relay Timer Blocks that change the On/Off states.
Important change in the Warfare Evolution update.
The Warfare Evolution update fixed the “AND gate checkbox” problem described here: https://support.keenswh.com/spaceengineers/pc/topic/28005-event-controller-does-not-properly-track-block-states-in-normal-or-mode
This means that You no longer need a workaround to setup the OR gate (however all the builds that use the workaround will still work), which also simplifies the building of XOR gate a tiny bit, the relevant sections of this guide have been edited to reflect the changes.

For completion sake I will also describe how to recreate the pre Warfare Evolution behavior which was:

“If ANY state blocks switch state to TRUE, execute actions TRUE, else if ANY state blocks switch state to FALSE, execute actions FALSE,”

To setup this We require two Event Controllers, lest name them “Logic ANY TRUE EC” and “Logic ANY FALSE EC”.

In “Logic ANY TRUE EC”:
- set the event to “Block On/Off Switched”,
- add as many state blocks to monitored blocks as you want (more then 1),
- leave AND gate checkbox unticked,
- trigger the TRUE actions in the first slot (example – turn a different state block On),
- leave the second action slot empty.

In “Logic ANY FALSE EC”:
- set the event to “Block On/Off Switched”,
- add as many state blocks to monitored blocks as you want (more then 1),
- tick the AND gate checkbox,
- leave the first action slot empty,
- trigger the FALSE actions in the second slot (example – turn a different state block Off).

I will also include the old workaround for the OR gate:

“If ALL state blocks switch state to FALSE, execute actions FALSE, else If ANY state blocks switch state to TRUE, execute actions TRUE”

This requires introducing the “anti state” (anti memory) blocks, because of the “Block On/Off Switched” limitation. Anti state blocks represent TRUE by being Off, and represent FALSE by being On.

The setup:
- set the event to “Block On/Off Switched”,
- add as many anti state blocks to monitored blocks as you want (more then 1),
- tick the AND gate checkbox,
- trigger the FALSE actions in the first slot (example – turn a different state block Off),
- trigger the TRUE actions in the second slot (example – turn a different state block On).
The NOT gate
The instances where a standalone NOT gate is required should be rare, if you exploit the multi toolbar feature to its limits, but the setup is simple:
- set the event to “Block On/Off Switched”,
- add exactly one monitored state block (the one you want to reverse),
- ignore the AND gate checkbox,
- trigger the FALSE actions in the first slot (example – turn a different state block Off),
- trigger the TRUE actions in the second slot (example – turn a different state block On).
The AND/NAND gate
The AND gate was already covered in the “basics” section of this guide, but it wont hurt to go through it again. The AND statement is:

“If ALL state blocks switch state to TRUE, execute actions TRUE, else if ANY state block change state to FALSE, execute actions FALSE”

The setup:
- set the event to “Block On/Off Switched”,
- add as many state blocks to monitored blocks as you want (more then 1),
- tick the AND gate checkbox,
- trigger the TRUE actions in the first slot (example – turn a different state block On),
- trigger the FALSE actions in the second slot (example – turn a different state block Off).

The NAND gate is the AND gate + NOT gate, the statement is:

“If ALL state blocks switch state to TRUE, execute actions FALSE, else if ANY state block change state to FALSE, execute actions TRUE”

The setup:
- set the event to “Block On/Off Switched”,
- add as many state blocks to monitored blocks as you want (more then 1),
- tick the AND gate checkbox,
- trigger the FALSE actions in the first slot (example – turn a different state block Off),
- trigger the TRUE actions in the second slot (example – turn a different state block On).

Using the multi toolbar feature one Event Controller can be both AND + NAND at the same time.
A working example can be seen in the “6:48 - AND/NAND gate setup tutorial” section of the tutorial video.
The OR/NOR gate
The OR statement is:

“If ANY state blocks switch state to TRUE, execute actions TRUE, else if ALL state blocks switch state to FALSE, execute actions FALSE,”

The setup:
- set the event to “Block On/Off Switched”,
- add as many state blocks to monitored blocks as you want (more then 1),
- leave AND gate checkbox unticked,
- trigger the TRUE actions in the first slot (example – turn a different state block On),
- trigger the FALSE actions in the second slot (example – turn a different state block Off).

The NOR gate is just OR gate + NOT gate, the statement:

“If ANY state blocks switch state to TRUE, execute actions FALSE, else if ALL state blocks switch state to FALSE, execute actions TRUE,”

The setup:
- set the event to “Block On/Off Switched”,
- add as many state blocks to monitored blocks as you want (more then 1),
- leave AND gate checkbox unticked,
- trigger the FALSE actions in the first slot (example – turn a different state block Off),
- trigger the TRUE actions in the second slot (example – turn a different state block On).

Using the multi toolbar feature one Event Controller can be both OR + NOR at the same time.
A working example can be seen in the “0:17 - OR/NOR gate” section of the supplement tutorial video.
The XOR/XNOR gate
The XOR gate is a circuit that needs to be build from other logic gates, the simplest combination is NAND + OR + AND, the statement:

“If ANY state block switch to a DIFFERENT state then the other state blocks, execute actions TRUE, else if ALL state block switch to the SAME state, execute actions FALSE”

The setup:
- setup NAND gate – name the Event Controller “Logic XOR NAND EC”,
- allocate a state block for the NAND output – name it “State XOR NAND TB”
- set the event to “Block On/Off Switched”,
- add as many state blocks to monitored blocks as you want (more then 1),
- tick the AND gate checkbox,
- in the first slot turn the “State XOR NAND TB” block Off,
- in the second slot turn the “State XOR NAND TB” block On,
- setup OR gate – name the Event Controller “Logic XOR OR EC”,
- allocate a state block for the OR output – name it “State XOR OR TB”
- set the event to “Block On/Off Switched”,
- add the same state blocks you added to the “Logic XOR NAND EC” to monitored blocks.
- leave AND gate checkbox unticked,
- in the first slot turn the “State XOR OR TB” block On),
- in the second slot turn the “State XOR OR TB” block Off),
- setup AND gate – name the Event Controller “Logic XOR AND Final EC”,
- set the event to “Block On/Off Switched”,
- add “State XOR NAND TB” and “State XOR OR TB” to monitored blocks,
- tick the AND gate checkbox,
- trigger the TRUE actions in the first slot (example – turn a different state block On),
- trigger the FALSE actions in the second slot (example – turn a different state block Off).

The XNOR gate is just XOR gate + NOT gate, the simplest combination to build it is OR + 2xNAND, the statement:

“If ANY state block switch to a DIFFERENT state then the other state blocks, execute actions FALSE, else if ALL state block switch to the SAME state, execute actions TRUE”

the setup:
- repeat the steps from the XOR setup until You get to the AND part,
- instead of AND gate setup second NAND gate – name the Event Controller “Logic XOR NAND Final EC”,
- set the event to “Block On/Off Switched”,
- add “State XOR NAND TB” and “State XOR OR TB” to monitored blocks,
- tick the AND gate checkbox,
- trigger the FALSE actions in the first slot (example – turn a different state block Off),
- trigger the TRUE actions in the second slot (example – turn a different state block On).

Using the multi toolbar feature in the “Final” Event Controller, one circuit can be both XOR + XNOR at the same time.
A working example can be seen in the “2:40 - XOR/XNOR gate” section of the supplement tutorial video.
T flip-flop and counting in binary
The T flip-flop is a circuit that is able to store memory and changes its On/Off state when triggered, it so happens that every memory/state block we been using so far is a T flip-flop, and this can be easily exploited.

Lest build a 4 bit binary counter, a circuit that can be in one of 16 states, and the state can be advanced (increased by 1) when the circuit is triggered.
Since 1 T flip-flop is a 1 bit counter, we will need 4 of them, lets name them “x1 state TB”, “x2 state TB”, “x4 state TB” and “x8 state TB”.
Now we need a way to trigger the state blocks in the correct order, lets start witch the “x1 state TB” which will be our input, to make it easy to extend we start by adding a relay timer “Step TB” and setup it to “toggle block On/Off” for “x1 state TB”, then we add a button and setup it to “trigger now” for “Step TB”, every time the button is pressed “x1 state TB” changes state.
Next step is to chain the rest of state TB’s, so we add 3 Event Controllers “x2 toggle EC”, “x4 toggle EC” and “x8 toggle EC”, set the event in all of them to “Block On/Off Switched”.
In “x2 toggle EC” monitor the “x1 state TB” and “toggle block On/Off” for “x2 state TB” at the second (if FALSE) action slot,
in “x4 toggle EC” monitor the “x2 state TB” and “toggle block On/Off” for “x4 state TB” at the second (if FALSE) action slot,
in “x8 toggle EC” monitor the “x4 state TB” and “toggle block On/Off” for “x8 state TB” at the second (if FALSE) action slot,
A working example can be seen in the “5:00 - binary counter” section of the tutorial video.
The “Step TB” can also be triggered by a clock input, resetting the counter to 0 would require setting all 4 state TB’s On and the advancing the counter by 1 step, more bits can be added by adding more segments to the chain continuing the pattern.

Triggering an action for a specific static value of the counter:
Lets say that we want to trigger an action and reset the timer when it reaches “10”, first we have to determinate what combination of states indicates 10, and that is: 1x8 + 0x4 + 1x2 + 0x1, so “x8 state TB” and “x2 state TB” should be On, and “x4 state TB” and “x1 state TB” should be Off. Note that counting starts from 0!
Since we need to be working witch “if FALSE” statements, we need to add 2 anti state blocks “x1 anti state TB”, “x4 anti state TB”, then add a "x01 NOT EC" (add “x1 state TB” to monitored blocks and in the first action slot turn “x1 anti state TB” Off, in the second scot turn “x1 anti state TB” On)”, and edit “x4 toggle EC” to also “toggle block On/Off” for “x4 anti state TB” at the second (if FALSE) action slot in other toolbar.
Reset the counter to 0 and manually turn the anti TB’s On, also edit the first step of the reset system to turn anti TB’s Off. Finally setup the AND gate:
- set the event to “Block On/Off Switched”,
- add “x1 anti state TB”, “x2 state TB”, “x4 anti state TB” and “x8 state TB” to monitored blocks.
- tick the AND gate checkbox,
- trigger the the actions you want and reset the counter in the first slot.

Working example: https://steamcommunity.com/sharedfiles/filedetails/?id=3025535970

Triggering an action for a specific dynamic value of the counter – compering two binary values:

A working example (for 2 bits) can be seen in the “vanilla multi image LCD display” video

To set this up you need a counter, a memory with as many bits as the counter and XOR gate for every bit.
First compare all equivalent bits wit each other using XNOR gates and store the results in additional anti state TB’s, then compare the results using a NOR gate, first slot triggering the actions you want when the values are the same, second slot when different.
Other Examples:
AI crane – custom toolbar actions and executing actions for specific rotor angle ranges:
also see the “2:20 - XOR gate used in practice” section in the tutorial video.

Decimal counter witch digital display – advanced and reliable sequencer
2 Comments
Wojak  [author] Apr 2 @ 7:51am 
My theory is that the developers simply added this feature to late in the games life and most people at this point are committed to using scripts, also most of average users don't write theyr own scripts, but download them from the workschop, whith is way easyer then fighting the in game interface to build something witch event controllers.
Space Engineer Apr 2 @ 3:58am 
Finally, thank you. I keep looking for some way to track iterations of an event happening, and there's almost nothing documented on this for SE, which blows my mind, considering the similarities (and frequent comparisons with Minecraft). I don't get how almost no-one seems to be interested in building systems like this, even when we have scripts available.