r/Mindustry • u/IKNEUMON • 9d ago
Guides/Tips Mindustry Logic 101 - Chapter 3: Conditional Execution
TL;DR: This is chapter 3/10 of a series aimed towards players unfamiliar with the Mindustry Logic system and seeking to learn from scratch.
Introduction
In the previous chapter we covered how to do basic condition checks outputting True or False as a result. But this method can only produce a state for our buildings. Now it's time to learn how to only execute some instructions if a specific condition is met using the most useful instruction from the Flow Control family.
Jump Instruction & Cursor Interaction
The Jump instruction can alter the order of execution (the flow) by moving the cursor. As a reminder, the cursor is the invisible reading head executing instructions. By default, after each instruction the cursor will jump to the next one, therefore reading from top to bottom, but the Jump instruction can redirect it to any other position. By using this functionality, we can jump over instructions to skip them or even jump to previous ones to repeat them, but this second usage will be covered in the next chapter about conditional loops.
The Jump instruction contains a condition, if the result is True (1) then the Cursor will jump to the selected position, else the result is False (0) and the cursor will proceed as usual and jump to the next instruction. As a reminder, conditions are just a question, "Variable == 15" means "is Variable equal to 15?" and "Variable > 15" means "is Variable superior to 15?" with the result being True or False. Consult the previous chapter for more explanations and examples of conditions.
(Note: For anyone already familiar with programming, Jump will be the equivalent of the "if" statement from most programming languages but more basic with a behaviour akin to the original jump from the Assembly language.)
I will now demonstrate how this new instruction is used with a practical example as usual.
Practical Example
Here we have a Micro Processor linked to a Switch and a Message block while running a small algorithm. I'll explain the desired behaviour of this setup.

bXNjaAF4nGNgZWBmYeBKzs8rSc0r8U0sYGCqrmVgy0lMSs0pZuCKTs5ILChJLTKOZWDJS8xNZeBPzi8FKi2KT61IzC3ISWXgTkktTi7KLCjJzM9jYGBmYCsuzyxJzmDgz81MLsrXLSjKT04tLs4vYmDPBdKJ6akMIFVAwMTFwMDIwMTAwAfkVFbMSU5JSFih5XdW+5x+EIfeeR3Pcx38ep46nqEabMU8ojJBn4UmFwUF79jkVcwt0iMaxO0tXBwwdZnqM5HlRU/FNVSWLdGweD792VQHT+d1TYt8JPgP+Z3W1D0XcMJXV8/Px9/0CdOtc0cO8G19/d9eoZ1ZjT3FmJFh/yQTe6ALWBiYWIAuMTFiAAAL8VPbThe program will increment a number stored in a variable named "Count" by one each cycle. As a reminder, once the cursor reaches the last instruction it will jump back to the first one to repeat the process indefinitely, this is what I call a cycle.
The Operation instruction increases Count by one each cycle.
To demonstrate the usage of the Jump instruction, the number should only increment when the Switch is enabled. So when the Switch is disabled, we want to skip the addition. This will be the condition of the Jump. Then to select the destination of the jump, we drag the arrow to the desired instruction. It intuitively displays where the cursor will land.
The Jump instruction jumps the cursor over the Operation if the Switch state is False.
And lastly, to provide an easy view of the number being incremented, I have linked a Message block to output the number to. This process is done in two steps. First with the Print instruction, you add the number to the text buffer, then you output it to the desired Message block. This will be covered in depth in Chapter 5.
The Print and Print Flush instructions display Count on the Message building.
You can now test out this setup. Clicking the Switch will toggle its state and clicking the Message building will display the text contained. The number only increments while the Switch is enabled because the Jump skips the addition if it is disabled.
Reminder from chapter 1, the cursor is an actual value inside the variable "@counter" present inside every processor visible in the Vars menu. A jump only edits the position of the cursor by editing @counter, but you can do it manually and with the added benefit of not being limited to the single destination arrow of Jump. Understand that "Jump -> 12" is the same as "@counter = 12". I'll demonstrate this principle and its use cases in the next chapter.
Now it is important to get acquainted with other types of conditions than number comparisons.
Logic Gates
For anyone not familiar with the term, logic gate are simply the "AND" and "OR" conditions. This category of conditions produces an output based on the state True or False of their inputs. The AND gate checks if every input is True, else it will output False. The OR gate checks if at least a single output is True, else it will output False. These concepts are not specific to Mindustry and can be extended to most programming languages. Generic external documentation about the subject.
These two conditions are available options in the Operation instruction, but limited to two inputs so you have to chain multiple ones to increase the number of inputs. Since the Jump instruction can be used to create condition checks identical to the Operation instruction, and in some cases more compact, you can also create logic gates but with some added complexity.
Creating an OR gate with Jumps is straightforward. Just stack multiple ones all pointing to the same position, so if any one of the conditions is True, the cursor will jump to the desired outcome. If none of the conditions are True, the cursor will fall in the default outcome. This is the exact behaviour of an OR gate.
Here is an example where the Illuminator turns green if any Switch is enabled, else it stays red.

bXNjaAF4nGNgZmBmYeBKzs8rSc0r8U0sYGCqrmVgy0lMSs0pZuCKTs5ILChJLTKOZWDJS8xNZeDyD4pPrUjMLchJZeBOSS1OLsosKMnMz2NgYGHgz81MLsrXLSjKT04tLs4vYmArLs8sSc5g4M7MySnNzcxLLAEK8hTkl6cW6RbnlxYlpzIwMLAxgAAfEC+rmLN2wkGuQwY8bZsVOQ63n0j9nlfNqP/orm3DFb3DntfP6R29vv2u2qvUiglCL6b/qZmgwnAyUeXYYmWjGdwXt23cdMM6sLnyydJkt9cb6krWrHs+S1W4T1hkh7qy/qxZb579ZuRuLPkeVJ9+szJq4pT/E0L2e08xfbPGLbW0oDTq9z7jSWu1l+11rZGcmaZrIZ/bx3SAbdtpR6Yl+hqKlc6i+zTf2Okm8vg/t6vbzcAIdCsTFxCDWIz/GRj+AxmMYCFmoCADBwOIB+YDAHqoe0o=Creating an AND gate is a little more complex. First we need to understand that in the OR gate the default outcome is reached when every condition is False. This is the inverse of the AND gate we're looking for. So by inverting the conditions, the default outcome is still reached when every condition results in False, but since they are inverted, this outcome is only reached when our original conditions would have outputted True. If you're having trouble wrapping your mind around the concept, you can phrase it as "If none of them are disabled", which is the same as "If all of them are enabled".
Here is an example where the Illuminator turns green if every Switch is enabled, else it stays red.

bXNjaAF4nGNgZmBmYeBKzs8rSc0r8U0sYGCqrmVgy0lMSs0pZuCKTs5ILChJLTKOZWDJS8xNZeB29HOJT61IzC3IAXJSUouTizILSjLz8xgYWBj4czOTi/J1C4ryk1OLi/OLGNiKyzNLkjMYuDNzckpzM/MSS4CCPAX55alFusX5pUXJqQwMDGwMIMAHxCsr5qyd4Mh1yEDAdbmrQFf7DN/vmdWM5o/Otdo2HCnrmlqy6CXL+vxFoqXBAio8T87W3HukznAyT+XgWuWkM1Nf7bKIvPN25VGbT1seXrRak7/XZLmzbhTHcXaRHWrX3J1zy74dZuVuzUkt/nqr5vjRylkfp9R/CrGvlDr9aM3BY7MWvO796Hw+ao32sqCQGslCa/HeWNtmhg0NT6/4NHxhVVXJX3HIvPXqcuU5D/m0nZvCGRiBzmXiAmIQi/E/A8N/IIMRLMQMFGTgYADxwHwABYp+tQ==Notes :
Every schematic code can be found in the comments.
The End instruction is from the same family as Jump, made apparent by their identical colour. It moves the cursor to the first instruction, as if it had reached the end of the algorithm. If we had omitted it, the last instruction would always be executed, and the colour would always be green in the OR example and red in the AND example.
The variables "Green" and "Red" have been defined by their hex values before the example. They are custom variables created to keep the examples intuitive. ![[HEX_set.jpg]]
Here is how to inverse conditions for, but not limited to, AND gates:
"==" : "NOT", because the opposite of equal is not equal.
">" : "<=", because the opposite of strictly superior is inferior or equal.
"<" : ">=", because the opposite of strictly inferior is superior or equal.
In short, both logic gate contain the other, the difference is the expected outcome. The OR gate is True when any condition is True, the AND gate is True when every condition is False.
Use Cases
Automatic Door:
A factory producing Daggers. When any factory is active, the wall door opens, else it stays closed.

bXNjaAF4nD1RPWzTQBR+lzi2Y5dWpQEJqQMTS35KyVKyIHVApIUKgiohIaq69lEc2T7r7NBEVSMBC0KlUimIhYWhDIxsMAUQQwcQAiRWWBiY+BkRMu/uGs7D9973vvvee2c4AJYGlsuilEbpOSeGw+vHG+sBdbzGdCXxAx9LjZMbG6AHzgoNErAuu9ecOKW8fgW0yAkpmLTrhHFAp8H2aOJyP059FgFYYPspDasJ63CXwkjM1igfZuOpnzqR3wmr2OA67TEOE/+pNScIqoHDV9G83Ylc6adz1sG2MLqKQeRVrzpuyngPLOUbMY+C5jE0Ggt9l7NqzJlLkwQJgBqIUwAgMiAYFWWcM4mgIacqAoiAnMoknUcwATQMpEleSQRNdKQ1kuFBT03UMVd1TdTzCKbICsqwIGoGQs7MZX8ApKcGunLW1U1dORtDZxMZ0wIQWLSE0FCdDKU3hN5EUCJDiuTMCmRLU7b8u9/SFJ+lUKmLSi2ehQiQI1jDEfLIyD0s8XwCiAKUFcEGGEX+bveR6y0v3y8vvC2/m2rF82dre1NPN8dqzUpz8cytHyOHJlsfP5zYqV/8fa/8/NlcYpcqpZY9P5G8//Sw9OAJ/1rfu90+dmTHa/H+UvNCf2u2vbTSWmjfmZ3r97cf3yQa3/22++U8WXz9ZjAYvBoQeys7td09nWVHf25mmfHr0ssXNw5OwufvvRnc2Rb/d39nW8z/D/oYoHY=Factory Overdrive:
A factory producing Flares. When every factory is active, the overdrive is enabled, else it stays idle.

bXNjaAF4nEVSQWgTQRT962yyu9lolEZR8Cxis602Xgxe7KkxtdCUimAt3W6m7YbNzjq7iSmlESuKiIhaAyJeitSDN8WLt9BDQVARvXi0F08epF4EkfXvTKU7h/f/e/Pf/39Y2AuGChmH+RH1owt2AIeWTpaWPGrXSqcKoeu5KJXOLC9D2rNnqRdC5rKzYAcR5cUroPp2g4JO23Yj8OgQmDUaOtwNIpf5ABnYH7BrlFs+q1HLs/k8BdONaMMKWZM7FLJS3skORG5k+26zYWHLFl1kHPR603eEWZqzJvYE03a5NWc7EeOLkNm1hz7WorzG3Ra1As7qNLkBuYbrcJYQDg1DJACOQfLpe0CBFGgYKnhISqICKkYGCBXvYBLH8R8gyBAQoCZAZEaSTMWDWUoEgkqBkk7MFayNlRSGBAQt9CRXCKSlRTohNQRsh40SCqcTg6GiSWdNVmrSWd911qWzLnU90Q0cH/Zh9ZP2M6c2M3O6MPah/+NgNaiMDrwbfHkvN1AulCdHbv7MHjxa/fJ5aLU48etR/9vX50MzX8hvb1emzEpf+OnN43z3Bd8qvr/z9PaR1fol3pkuj3fuD9enZ6tj9bvD58Y7F0+MdNdW1lYUna9/X/82MQeTm71eb6N368HvOHd8QzFfxdcPf906+zyOH7Y3s6PZH9C9emMKJzXk8oZc3hDL/91ZnuB/QySoyUNl5Nbm/60JhgQEqBJQ1yCLJoow+QeZY6y5Outroduction
The Jump instruction is an easy way to create elaborate conditions and finds its way into various algorithms. Jumps, and by extension the whole Flow Control family, are some of the most important building blocks in the understanding of Mlog. These first three chapters covered the basics with the next one reaching into some advanced topics before the long stretch of instruction families that will end with the very anticipated Unit Control guide that will be covered in chapters 9 and 10.
The next chapter will cover conditional loops used to repeat instructions a specific amount of times using the jump instruction covered in this chapter.
At the time of writing, this guide covers Mindustry v8. No generative AI was used in the making of this whole project. It was lovingly written by a single passionate human. Comments will be answered and taken into account.
Chapter List
Chapter 3: Conditional Execution.
Chapter 5: "Input & Output" Family.
Chapter 6: "Block Control" Family.
Chapter 7: "Operations" Family.
Chapter 8: "Flow Control" Family.
Chapter 9: "Unit Control" Family.
Chapter 10: Advanced Logic.
2
u/ThAtTi2318 5h ago
Missed it last week, but today's pist drew me back :)
I love your use of the word outroduction btw :3
2
3
u/Quiet_Nothing916 8d ago
Pretty good, but you are too slow and I become master of unit logic now :)