Mindustry

Mindustry

Not enough ratings
[LOGIC] How to use @counter
By Highfire1
Advanced Logic Series - @counter

An in-depth guide on what @counter is and how to use it.
   
Award
Favorite
Favorited
Unfavorite
What is @counter?
@counter is a built in variable that tracks the current line number of code that a processor is running. In other words, given a program like this:


@counter will start at 1 and print, then it will increment to 2 and printflush, then reset to 1.

The main use of @counter is to create pseudo-functions.

Note that unlike getblock, @counter is 1-indexed.

What's important is that @counter can be read and modified, e.g.


The above will flush 1 to the message.


And the above will skip the second line.
Using @counter
As alluded to in the first section, the main use of @counter is to create functions.

there are two important components to creating functions:

The first is setting the address of your function:
The functions themselves are composed of only 2 lines: a print and a printflush. What's important is the infrastructure - using jumps and operations to set the addresses of the functions.


The second component is a callback, so that you can run multiple functions:
(note that the ends are redundant, but they help to keep the code organized)
op add func1_addr @counter 1 jump 5 always x false print "function1" set @counter callback end op add func2_addr @counter 1 jump 10 always x false print "function2" set @counter callback end op add callback @counter 1 set @counter func1_addr op add callback @counter 1 set @counter func2_addr printflush message1

That's it!
1 Comments
Bunby 13 May, 2024 @ 8:35am 
I'm a little confused, on the application, as you didn't explain what you were doing / why in the second half, but otherwise very useful information!