Skip to content

Variables and names

A variable isn't declared — it's just named. Which names are free, and which are already taken

mlog has no line that says “declare a variable”. A variable comes into being the moment its name first appears in the program — whether in a field written to or in a field read from.

Everything else in this lesson follows from that.

Not to a line, not to a piece of the program, not to an instruction — to the whole processor. Every line sees every variable; mlog has no scopes at all.

That is why names have to be meaningful: fifty lines later, the x from the top of the program is still the same x.

A neighbouring processor is a different set of variables, even if the names match. They share nothing: passing a value needs memory or a link, and those get groups of lessons of their own.

stock and Stock are two different variables. mlog has no “case-insensitive” comparison of any kind.

tick 0
Set0
=
Set1
=
Operation2
=
Set3
=
@counter0number
stocknullnull
Stocknullnull
totalnullnull

Press “step” four times. Both variables show up in the table — 100 and 5 — and total becomes 105. The program does not consider this an error and does not warn: mlog has nothing to warn with.

Hence a simple rule: pick one way of writing names and stick to it. Non-Latin letters work in names just as well as Latin ones — the game stores the program as UTF-8 text.

A block’s field is not a text editor: the game replaces some characters as you type, so that a name cannot break the way the program is written down.

What you typed What you get
space, tab, # _
; s
" '

So my stock turns into my_stock by itself. This is not the editor being fussy: the program is stored as text, its lines are separated by newlines, and # starts a comment — a space inside a name would make two variables out of one.

A name starting with an at-sign belongs to the game: @copper is an item, @time is time, @thisx is the processor’s coordinate. Such names are read-only: an assignment to them goes nowhere, silently and without consequence.

The fourth line of the example is set @copper 7. It does nothing, and @copper never appears in the variables table at all: constants do not go there.

Three names without an at-sign are taken too: true, false and null. They are constants as well — 1, 0 and emptiness — and writing to them fails in exactly the same way.

There is no limit: as many names as appear in the program, that is how many there are. What is limited is the program itself — 1000 instructions, and at that limit the buttons that add a line go dark.

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.