Skip to content

Format

A whole template instead of text in pieces: format drops values into a ready caption

print assembles a caption in pieces, and on a long phrase that becomes awkward: the text is split across lines of the program, and what it will finally look like exists only in your head.

format works the other way round: first the whole template goes into the buffer, then values are substituted into it.

tick 0
Sensor0
=
in
Print1
Format2
Format3
Print Flush4
Wait5
@counter0number
coppernullnull
textBufferstring

Press “run”. Under the message block you get copper 120 of 300: the template copper {0} of {1} went into the buffer first, and two formats replaced its placeholders in turn.

A placeholder looks like {0}: an opening brace, a digit from 0 to 9, a closing brace. No spaces, no numbers above nine — such a thing simply does not count as a placeholder, and format will not see it.

The smallest number wins, not the first placeholder

Section titled “The smallest number wins, not the first placeholder”

This rule surprises people. format searches the buffer for the placeholder with the smallest number, not the one that comes first.

For the template {1} and {0} the first format substitutes at the end — where {0} is. The order of substitutions is set by the numbers, not by their position in the string.

A replaced placeholder is gone from the buffer, so the next format finds the next one by number. Two values cannot go into one placeholder, and an extra format with none free does nothing.

What is in the buffer format 120 gives
copper {0} copper 120
{1} and {0} {1} and 120
copper 0 copper 0 — no placeholder, the value is lost
empty empty — nowhere to substitute

Values turn into text by the same rules as with print: a whole number prints without a fraction, a building as its block’s name, emptiness as the word null. That is the lesson “What prints as what”.

Unofficial fan project, not affiliated with Anuke. Mindustry sprites, fonts and translations © Anuke, used under GPL-3.0; Fira Code under OFL-1.1. Site code is GPL-3.0.