Skip to content

When it does not work

mlog reports no errors. So you have to look yourself — and there is an order in which it is done

A program with a mistake in mlog does not crash, does not turn red and writes nothing in the corner of the screen. It simply computes something other than what you meant — and does it sixty times a second, confidently and silently.

So you have to find the cause yourself. The good news: there are not many ways to break a program at this stage, and all of them are recognised at a glance in the variables table.

What you see Where to look
nothing happens at all whether the program runs: is @counter stuck or moving
it computes, but wrongly the values: which one first stopped being what you expected
it works, then breaks the iteration: from the second pass the variables are not empty

The third is the sneakiest, and the most common. The program runs in an endless loop, and on the second iteration the very same thing runs in different surroundings.

The main technique is not to run the program but to walk it one instruction at a time. “Run” shows only the consequences; a step shows which line created them.

The order is:

  1. “Restart” — reset everything so you start from a clean slate.
  2. Step — one instruction. Look at the table after each step.
  3. Find the first line after which a value stopped being what you expected. The mistake is in that line or above it; looking below is pointless.
  4. If it never even reaches the line in question — look at @counter: it shows which line will run next.

This is exactly what the game does not have: there a processor either runs or is switched off. Step is our button, and we keep it precisely for this.

An example: a counter that counts the wrong thing

Section titled “An example: a counter that counts the wrong thing”

The program below was supposed to count how many of ten are done and what share that is. Press “restart”, then “step” four times and look at the table.

tick 0
Set0
=
Operation1
=
Operation2
=
Operation3
=
@counter0number
aimnullnull
countnullnull
leftnullnull
sharenullnull
everythingnullnull

After the first iteration: aim is 10, count is one, left is nine. So far it looks right. But share is empty, though it should be a number.

Now press “run” and wait a couple of seconds. The picture changes: count has passed ten and keeps growing, left has gone negative, share is still empty.

There are two different mistakes here, and both are typical.

share is empty because nobody filled in everything. In the line op div share count everything the divisor is a variable that was never assigned anything, that is, null. In arithmetic null counts as zero, and division by zero in mlog gives neither infinity nor an error: the result is simply “nothing”. An empty variable in an answer almost always means something that went into it was empty.

count grows without stopping because the program runs in an endless loop. op add count count 1 adds one to what has accumulated from the previous pass, and there are sixty passes a second. The line itself is correct — what is wrong is the expectation that it runs once.

  1. A typo in a name. An extra row appears in the table with a nearly identical name and the value null — in detail in the lesson “The variables window”.
  2. An empty variable in a calculation. null does not complain, it counts as zero: addition gives back the other term, multiplication gives zero, division gives emptiness. Look not where it is empty but where it should have been filled.
  3. A forgotten iteration. A variable you expected to be empty arrives with the value from the previous pass. One press of “restart” checks it: if the first iteration is right and everything drifts after that, the iteration is to blame.

No error messages. Not in the block, not in the corner of the screen, not in a log — they do not exist as a phenomenon. The only source of truth is the values of the variables.

No “broken” program. A line the game does not know does not break the others — it simply never runs, while you hunt for why a piece of the logic vanished. Where such a line comes from is told in the lesson “A program is text”.

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.