Logic Usage
2026-05-08
Logic Usage — Mindustry guide with processor commands, code examples, and programming tutorials
Source: 使用方法 (wikiwiki.jp, source attribution)
Logic / Usage
Last-modified: 2023-05-09 (Tue) 20:35:14
Keywords Highlighted in This Guide
Sample programs have been moved to Logic / Sample Programs
- Code Notation Guide
- From Reddit(?)
- Common Usage
- Write
- Read
- Draw, DrawFlush
- Print, PrintFlush
- GetLink
Radar- Control
- Sensor
- Set
- Operation
- Jump, End
- UnitBind
- UnitControl
- UnitRadar
- UnitLocate
Code Notation Guide
sensor result = @copper in block1
set A = 100
op sub result a b
In the above case, apply markup according to the following rules:
- Purple: Fixed commands
- Maroon: Selectable subcommands
- Black: Variables; you can use any alphanumeric name as long as it doesn't conflict with other variables (single digits or leading digits may not be allowed in some cases)
- Green: Built-in variables; marked with @ at the beginning and can be selected from the pencil icon
- Teal: Link names; the name displayed when linked to a processor
- Olive: Values; numbers, text, or any arbitrary value
From Reddit(?)
Throughout this guide, we use various code snippet samples (for example, op add xx 10). To verify these features, copy them to your clipboard and then click [Import from Clipboard] on the processor to insert them into the processor. Needless to say, it's easiest to follow this guide on a PC.
The following paragraph is for people who have no idea how code works at all. If you already know, feel free to skip it.
Processors are very much based on assembly language, the language that IRL processors use at the most basic level. To understand how assembly language works, imagine a very clever monkey. He has a list of instructions. He looks at it and does each one in order. Every time he reaches the end of the list, he starts over from the beginning. He does nothing other than follow instructions. If he sees an instruction like (set x 50), he will understand that (x is 50). Later, when he sees the instruction (op add x x 30), he will remember that (x is 50, and if I add 30 to that x, it becomes 80). Now, try editing the variable value in the following code snippet and connect it to a message block (see the next section). You don't need to worry about the last 2 instructions. It just displays a number on the message block.
set x 30
op sub x x 10
op add x x 0
print x
printflush message1
Now that you understand how code works, how do you use a processor? First, click to select the processor (using PC controls; if playing on mobile, replace click with tap). Then click another building to connect to it. You can connect to almost anything you can imagine: containers, conveyors, pipes, combustion generators, cyclones, cultivators, copper walls, coal centrifuges, cryogenic fluid mixers, command centers, and sometimes even things that don't start with "C". Once you've connected everything you want to connect, click the pencil icon to edit the code. You can use the "Add" button to add instructions, use the "Edit" button to import from and copy to clipboard, and use the "Back" button to exit. Next, we move to the actual instructions.
"Read" and "Write" instructions read from and write to connected memory cells. You simply specify the address you want to write to, and another (or the same) processor can read from that address and get the value back. "Draw" and "Print" instructions are added to the processor's draw and print buffers. "DrawFlush" and "PrintFlush" instructions send the entire contents set by Draw and Print to the display or message block and then clear that buffer. The "Draw" instruction actually has several variations, such as clearing the entire display with one color, drawing lines, drawing polygons, and changing the color of all future drawing commands. Try it out for real. We'll skip some complex instructions we don't want to explain (figure them out yourself), so moving on to "Set", which sets variables to specific values (no need to declare variables, just use them). Using the "Operation" (or just op) instruction, you can perform math operations on variables. If a condition is true, "End" jumps to the beginning of the instruction queue, and "Jump" jumps to where the arrow points.
As another example, try setting up a processor with this code and connecting it to a logic display:
set var 0
op mul red var 3
draw color red 0 blue 255 0 0
draw rect 0 var 80 2 0 0
op add var var 1
drawflush display1
jump 1 lessThan var 81
op add blue blue 20
op mod blue blue 255
It cycles through a loop, creating a stack of thin rectangles that gradually become redder as you go up. When it reaches 80 (the height of the display), the next cycle becomes more blue and repeats. Please try it!
Common Issues / Processor Q&A:
Q: Why doesn't the processor work?
A: Hyper Processors require cooling water, and you must use flush instructions (DrawFlush / PrintFlush) to display anything on a display or message block.
Q: Where is the processor in the tech tree?
A: Mech Drill >
Coal >
Graphite (Press) >
Silicon (Smelter) > Processor Section
Q: Why is this OP / Difficult?
A: Very complex if you don't know how to code / Very simple if you know how to code.
Source: https://www.reddit.com/r/Mindustry/comments/jf2k9t/how_to_use_the_new_60_processors/ (reddit)
Common Usage
- Build a Micro (or Logic, Hyper) Processor near the block you want to control
- Select the processor, then select the block you want to control to link them (OK if a blue square and link name appear)
- Click the pencil icon on the processor, and when the edit screen appears, enter your commands
- Once you click "Back" after entering, the program begins execution
Write
Used to write information to memory cells or memory banks
Setup Example
Link the memory cell or memory bank you want to write to
Code Example
write 200 cell1 0
200: The value to write (numbers only); you can also input a variable created with another commandcell1: The link name of the memory cell or memory bank to write to0: The address of the memory bank (like a variable name); you can use strings in addition to the default numbers
Read
Used to read information from memory cells or memory banks
Setup Example
Link the memory cell or memory bank you want to read from; the message block on the right is an example to display the result of reading
Assume the memory cell has 200 recorded at address A (variable), as per the write section
After Command Execution
Select the message block to see the value displayed
Code Example
read result cell1 A
print result
printflush message1
result: Variable to store the read result; any string is finecell1: The link name of the memory cell or memory bank to read fromA: The address of the memory bank to read (like a variable name)
See the respective sections for how to use print and printflush
Draw, DrawFlush
Used to draw shapes on displays (Logic Display,
Large Logic Display)
Commands
- draw: Set the content you want to draw on the display (you can create complex shapes by combining multiple commands)
- drawflush: Output the content set by draw to the display (the accumulated settings are reset)
Subcommands
clear: Fill the entire display with a single color (draw commands before this line are ignored)
r,g,b: Set color using numbers 0–255 for red (r), green (g), and blue (b) respectively (searching for "RGB" will find color palette tables as reference)
color: Specify the color of shapes drawn in subsequent draw commands on this line and beyond
r,g,b: Same as described in cleara: Transparency (alpha value); specify with a number 0–255; the closer to 0, the more transparent
stroke: Specify the line thickness of shapes drawn in subsequent draw commands on this line and beyond
- Value: Specify line thickness in pixels; upper limit is 1000?
line: Draw a line
x: x-coordinate (horizontal axis) of the start point of the line; left side of display is 0, right side is 80 (Large
Logic Display is 179)y: y-coordinate (vertical axis) of the start point of the line; bottom of display is 0, top is 80 (Large
Logic Display is 179)x2: x-coordinate of the end point of the line; same definition as xy2: y-coordinate of the end point of the line; same definition as y
rect: Draw a filled rectangle
x: x-coordinate of the bottom-left of the rectangle; same definition as x in liney: y-coordinate of the bottom-left of the rectangle; same definition as y in linewidth: Width of the rectangle in pixelsheight: Height of the rectangle in pixels
lineRect: Draw an outline rectangle
x,y,width,height: Same as rect
poly: Draw a filled polygon (adjust corners to draw triangles, squares, pentagons, circles, etc.)
x: x-coordinate of the center of the polygon; same definition as x in liney: y-coordinate of the center of the polygon; same definition as y in linesides: Number of corners; 3 for a triangle, and the more numbers, the closer to a circleradius: Radius of the polygon in pixelsrotation: Rotation angle (default is with a corner pointing up)
linePoly: Draw an outline polygon
x,y,sides,radius,rotation: Same as poly
triangle: Draw a filled triangle
x,y,x2,y2,x3,y3: Specify the coordinates of the 3 vertices of the triangle; same definition as x, y in line
image: Display an image (only item icons like ore can be used?)
x: x-coordinate of the center of the image; same definition as x in liney: y-coordinate of the center of the image; same definition as y in lineimage: Specify the item name to use as an iconsize: Specify the image size in pixels (common to x and y)rotation: Specify the rotation angle of the image
Setup Example
Link the display you want to draw on to the processor (left shows a display that is not linked and just placed)
Code Example
draw clear 0 0 0 0 0 0
draw stroke 10 0 0 0 0 0
draw lineRect 10 10 60 60 0 0
draw color 255 255 255 255 0 0
draw image 40 40 @copper 32 0 0
drawflush display1
Print, PrintFlush
Used to output text to message blocks
Commands
- print: Set the values to be stored in the message box
- printflush: Output the values set by print to the message box (the text string set in Print is reset)
Code Example
print "frog"
printflush message1
Code Example (Advanced)
Print can concatenate repeated strings until PrintFlush is executed, so if you want to concatenate a string + variable, you can execute Print with the string and Print with the variable, then PrintFlush to display the concatenation result
GetLink
Get links by index (serial number), useful for shortening code in loop processing, etc.
Indices are assigned in serial order 0, 1, 2... starting from 0 in the order they are linked to the processor, regardless of building type
Setup Example
Link 8 Duos together
Shoot in the same direction all at once
Code Example
Capture shows Jump as ≤8, but 7 is correct
set i 0
getlink Link i
control shoot Link 0 0 1 0
op add i i 1
jump 1 lessThanEq i 7
end
Give GetLink 0 to specify
Duo 1 and shoot, add 1 to i to specify the next
Duo 2... repeat until 7 (Duo 8)
Radar
Discover entities such as units; can only be set on turrets with range?
Subcommands
from: Specify the link name or variable of a shootable turret
target: Refine by combining 3 conditions with &
any: Anything (if only 1 condition, input in 2nd and 3rd condition fields)enemy: Specify enemiesally: Specify alliesplayer: Specify the unit operated by the playerattacker: Specify units capable of attackingflying: Specify flying unitsboss: Specify bosses (presumably Guardians and similar with health bars displayed)ground: Specify ground units
order: Specify 0 or 1 to set the Sort condition below in ascending or descending order (if distance, 1 prioritizes closer, 0 prioritizes farther)
sort: When multiple target units are found, the condition for aiming priority
distance: By distance from block (turrets determine priority within their range)health: By current healthshield: Does it have shield functionality?armor: Does it have armor functionality?maxHealth: By maximum health
output: The variable name to store the discovered unit information
Setup Example
Link with a turret such as
Duo
Code Example
radar ally any any distance duo1 1 A
sensor X A @x
sensor Y A @y
control shoot duo1 X Y 0 0
Unit position cannot be specified with the unit information stored in A, so use sensor to get @x @y and use it with control
- This logic makes
Duo track the nearest allied unit
Control
Used to control blocks
Subcommands
enabled: Set availability; 1 (or true) for available, 0 (or false) for unavailable. Also, the following special blocks have different behaviors:
shoot: Shoot from shootable blocks; specify the block you want to shoot in of, specify the coordinates to aim at in x and y, 1 for shoot / 0 for shoot off
shootp: Similar to shoot but can specify units? Additional information needed.
configure: Configure blocks that display a menu when clicked. Confirmed as follows:
Sorter,
Unloader: Using built-in variables like @copper, you can specify the item to be unloaded; 0 unloads nothing (unloader unloads all types)
Ground Factory,
Air Factory,
Naval Factory: Using built-in variables like @mono, you can specify the unit to be produced; 0 produces nothing
Setup Example
Configure version; link the unloader to the processor
Code Example
control configure unloader2 @titanium 0 0 0
Change the unload target to titanium; after this, even if you change the unloader settings directly with the mouse, it will revert to titanium
Sensor
Get information from a specified block or unit
Built-in Variables
Items
@copper:
Copper@lead:
Lead@coal:
Coal@graphite:
Graphite@sand:
Sand Floor@silicon:
Silicon@metaglass:
Metaglass@scrap:
Scrap@titanium:
Titanium@plastanium:
Plastanium@thorium:
Thorium@phase-fabric:
Phase Fabric@surge-alloy:
Surge Alloy@spore-pod:
Spore Pod@pyratite:
Pyratite@blast-compound:
Blast Compound
Liquids
@water:
Shallow Water@slag:
Molten Slag@oil:
Oil@cryofluid:
Pooled Cryofluid
Sensor Information (free interpretation)
Get the quantity of objects in blocks or units
@totalItems: Number of items held@itemCapacity: Maximum item capacity@totalLiquids: Total liquids@liquidCapacity: Maximum liquid capacity@totalPower:
Battery current value (single block)@powerCapacity:
Battery maximum value (single block)@powerNetStored:
Battery current value (connected blocks)@powerNetCapacity:
Battery maximum value (connected blocks)@powerNetIn: Total power generation in power network@powerNetOut: Total power consumption in power network@ammo: Ammunition held@ammoCapacity: Maximum ammunition capacity@payloadCount: Payload amount
Get object information in blocks or units
@firstItem: First (leading) item
Get information about blocks or units
@health: Durability@maxHealth: Maximum durability@heat: Temperature@efficiency: Efficiency@rotation: Angle (placement orientation?)@team: Team (for multiplayer?)@type: Type (?)@flag: Flag (used as unique ID for blocks or units)@name: Player name operating the unit; default: null@config: Configuration (?)@payloadType: Payload type (block or unit?)
Get position and angle information
@x: Horizontal coordinate@y: Vertical coordinate@shootX: Shoot target horizontal coordinate@shootY: Shoot target vertical coordinate@mineX: Mining position horizontal coordinate@mineY: Mining position vertical coordinate
Get block or unit status (true or false)
@shooting: Is it shooting?@mining: Is it mining?@controlled: Is it being controlled by UnitControl? 0=no 1=yes@commanded: Is it being led by a player-operated unit? 0=no 1=yes
Setup Example
Link the block you want to read information from; as an example, link it to a message block (message1) to display the result
If it's a unit, no linking is required if units are being produced
Display the amount of copper in the container on the message block (updates in real-time)
Code Example
sensor result container1 @copper
print result
printflush message1
After UnitBind execution, the container1 location can specify the unit with @unit to get the unit's information
Set
Used to set variables (which store values) for use in commands
Code Example
Same as Code Example Advanced in Print, PrintFlush
The first Set command sets the value 150 to the variable R
- The 3rd Print command prints R
- The benefit is that when the value changes, you only need to change the Set value and don't need to change the R in other commands, making it easier to apply
Operation
Used to perform calculations and store results in variables
For detailed logic, see this link on Github
Note: The
Arc imported above is a library provided by the Mindustry developer, and math-related content is stored here
Subcommands
Only those with different notation between screen and clipboard data are noted with clipboard in parentheses
Returns calculation result
+(add): Addition-(sub): Subtraction*(mul): Multiplication/(div): Division (calculated to decimal point)//(idiv): Division (truncates decimal point)%(mod): Remainder (outputs only the remainder of division)^(pow): Exponentiation (calculations like 3 to the power of 5)
Returns 1 if Yes, 0 if No
==(equal): Equal tonot(notEqual): Not equal toand(land): Both conditions are met<(lessThan): Less than<=(lessThanEq): Less than or equal to>(greaterThan): Greater than>=(greaterThanEq): Greater than or equal to
Returns binary operation result
<<(shl): Left shift>>(shr): Right shiftor: Bitwise OR (bit operation)b-and(and): Bitwise AND (bit operation)xor: Bitwise XOR (bit operation)flip(not): Bitwise NOT (bit operation)
Returns calculation result
max: Returns the larger valuemin: Returns the smaller valueatan2: Angle of two points on coordinates (Mathf.atan2)dst: Range of coordinates? (Mathf.dst)noise: Noise?abs: Absolute value (if -1 or 1, then 1)log: Logarithm (base e)log10: Logarithm (base 10)sin: Sinecos: Cosinetan: Tangentfloor: Division (truncates decimal point, same as idiv but goes through float type, so errors may occur when results exceed float precision)ceil: Rounds up (returns the smallest integer greater than or equal to the given number; 0.001 becomes 1)sqrt: Square rootrand: Random
Jump, End
Used for conditional branching
Commands
Jump: If a certain condition is met, jump to the command indicated by the arrow; if not met, execute the next line command
End: End the command at this line
Jump Subcommands
==: Equal tonot: Not equal to<: Less than<=: Less than or equal to>: Greater than>=: Greater than or equal toalways: Jump regardless of condition
Code Example
Code to make
Duo shoot based on switch on/off
- The Sensor in line 1 gets the on/off status in Sw
- The Jump in line 2 jumps to Control if Sw is ON (1), moves to the next line (End) if OFF
- Line 3 ends the logic if OFF
- Line 4 makes
Duo shoot if ON
sensor Sw switch1 @enabled
jump 3 equal Sw 1
end
control shoot duo1 0 0 1 0
UnitBind
Specify a unit type and bind (make it controllable by logic / reserve?) the unit
UnitBind is required beforehand
Each time UnitBind is used, a different unit of that type is bound, so you may accidentally bind all units of that type at once
Built-in Variables
@dagger:
Dagger@mace:
Mace@fortress:
Fortress@scepter:
Scepter@reign:
Reign@nova:
Nova@pulsar:
Pulsar@quasar:
Quasar@vela:
Vela@corvus:
Corvus@crawler:
Crawler@atrax:
Atrax@spiroct:
Spiroct@arkyid:
Arkyid@toxopid:
Toxopid@flare:
Flare@horizon:
Horizon@zenith:
Zenith@antumbra:
Antumbra@eclipse:
Eclipse@mono:
Mono@poly:
Poly@mega:
Mega@quad:
Quad@oct:
Oct@risso:
Risso@minke:
Minke@bryde:
Bryde@sei:
Sei@omura:
Omura@alpha:
Alpha@beta:
Beta@gamma:
Gamma
Code Example
unitbind @flare
unitcontrol move @thisx @thisy 0 0
Line 1 specifies
Flare
- Line 2 is a command to move to the processor's position
UnitControl
Used to command the behavior of bound units
The explanation from Reddit is more detailed (in English)
Subcommands
stop: Stop movement, building, and mining
move: Move to specified coordinates (x, y)
approach: Move to a position at a specified radius (radius) centered on specified coordinates (x, y)
boost: Turn boost ON (1) or OFF (0) for boostable units (Nova, etc.)
pathfind: Move along the default path of ground units toward the enemy spawn point
target: Aim at specified coordinates (x, y); shoot if shoot is 1, stop shooting if 0
targetp: Aim at a specified unit (specified by UnitRadar, etc.); shoot if shoot is 1, stop shooting if 0
itemDrop: Drop a specified amount (amount) of items to a specified building (to)
itemTake: Get a specified amount (amount) of a specified item (item) from a specified building (from)
payDrop: Release or place a unit or building at the current unit position
payTake: Store the unit (1) or building (0) specified in takeUnits to the unit
mine: Mine at specified coordinates (x, y) (note that different units can mine different types of ore)
flag: Specify a flag (like an ID) to the unit so you can specify and control only one of multiple units of the same type
build: Build the specified block (block) at the specified coordinates (x, y) in the specified orientation (rotation) and configuration (config)
getBlock: Return what kind of block exists at specified coordinates (x, y) to the type (type) and building name (building) variable; coordinates must be placed within 5 tiles of the unit
within: Return to result whether the unit is within a radius (radius) centered on specified coordinates (x, y) (1 if yes, 0 if no)
UnitRadar
Find units that meet specific conditions and return results to a variable
UnitBind is required beforehand
Subcommands
target: Refine by combining 3 conditions with &
any: Anything (if only 1 condition, input in 2nd and 3rd condition fields)enemy: Specify enemiesally: Specify alliesplayer: Specify the unit operated by the playerattacker: Specify units capable of attackingflying: Specify flying unitsboss: Specify bosses (presumably Guardians and similar with health bars displayed)ground: Specify ground units
order: Specify 0 or 1 to set the Sort condition below in ascending or descending order (if distance, 1 prioritizes closer, 0 prioritizes farther)
sort: When multiple target units are found, the condition for aiming priority
distance: By distance from block (turrets determine priority within their range)health: By current healthshield: Does it have shield functionality?armor: Does it have armor functionality?maxHealth: By maximum health
output: The variable name to store the discovered unit information
UnitLocate
Discover specific blocks (buildings)
Subcommands
find: Specify the discovery target
ore: Ore; specify the ore with ore, return discovery coordinates to outX, outY and 1 to Found if found, 0 to Found if not found
building:
Block (building); specify the building type with type, specify enemy true (or 1) or ally false (or 0) with enemy, return discovery coordinates to outX, outY, 1 to Found if found, and link name? to Building
spawn: Enemy spawn point; options are the same as building
damaged: Damaged building; options are the same as building
Door
Switch