Advanced Moan Programming

Moan programs can use simple variables called 'registers', registers can only store integers.

Each moan channel has access to 10 local registers that are private to it, and 26 global moan registers that are shared with all other moan channels.

Local registers are referred to by R0 - R9. Global registers are referred to by RA - RZ. Note the use of capitals.

You can assign a register using the Let command (or 'L'), as so:

str myMoanProgram = "L R0=5*30"

Once assigned, you can make use of the values in your movement or animation commands:

str myMoanProgram = "Let R0=10 Let R1=4 Move R0,R1,1"

This command moves right 10 pixels and down 4 pixels at a speed of 1 pixel per frame.

There are also 3 special registers your program can access, these are called X, Y and I and hold the x,y location and the image index of the first sprite/viewport on the channel.

For viewports, I is always -1 and has no affect if assigned.

To set a sprite/viewport's X and Y location to 10, 50 you would do:

str myMoanProgram = "L X = 10; L Y = 50"

Using 'F'or Loops

Moan programs can also use For loops. These aren't as flexible as the for loops in TNT Basic, as they can only have a step of +1, but otherwise they are just as useful.

You could use a For loop instead of a Move command to set a sprite's location by assigning it repeatedly inside a loop. See the following example:

str myBrokenMoanProgram = "For R0 = 0 To 300; Let X = R0; Next"

You will find however, that if you run this program the sprite will fly across this screen almost instantaneously. This is because the moan program is running much faster than the screen is being updated, and so you only see the sprite at its start and end position, and nowhere inbetween.

To fix it you must add a Pause command into the program. A Pause command causes the moan program to wait until the frame has been drawn before continuing. Pauses are not needed when using Move or Animate commands, but when directly changing X,Y or I registers, you have to pause so the results get seen.

str myFixedMoanProgram = "For R0 = 0 To 300; Let X = R0; Pause; Next"

Accessing Channel Registers From Outside the Moan Program

You can read/write registers for a moan channel with the Set Channel Register and Channel Register commands. This is useful to allow you to alter the behaviour of moan programs on the fly.

See the Channel Register and 'Set Channel Register help pages for more information

Using Labels and Jumps

Moan programs can also make use of labels and goto statements (which are called Jump statements in moan). As with commands, labels are composed of a single UPPER case letter. This limits each program to having a maximum of 26 labels. A label is defined as such:

str myInfiniteLoop = "Begin: Jump Begin"

The Jump statement here will jump to the label at the beginning of the program, causing the program to get stuck in a loop.

However, if you were to run this program on a channel, TNT Basic would not get stuck in an infinite loop, even though the channel would. This is because each moan channel runs in parallel to your main program, so if one channel is stuck in a loop, it doesn't affect other channels or your main program (except maybe making things a little slower).

Wind (Again)

The previous wind example in the Simple Moan Tutorial used a Move command to move all the sprites to the right. The problem with this command was that they would only move 10000 pixels, then they would stop. What you really need is a program that loops so that the wind will never stop, here it is:

str myNewWind = "Begin: Move 1,0,-2; Jump Begin"

Here -2 is used as the speed. Using a negative speed means complete the movement in that many steps rather than at that speed. So using -2 instead of 2 means do the movement in 2 steps, rather than at 2 pixels per frame. This means the wind will move the sprites 1 pixel in two steps, or half a pixel a frame, much more of a gentle breeze!
As soon as the movement is complete, it starts again.

Using If Statements

Moan programs can also make use of very simple If/Then statements. An If statement in moan can only have a Jump statement as its body, so all moan If statements take the form: "If condition, Jump label".

The conditions are the same as TNT Basic's normal expressions, you can use =, >, <, <=, >= and <>.

str myMoanProg = "If R0>R1 Jump A; Move 50,50,1 A: Move -50,-50,1"

This program will skip a move statement if R0 is more than R1. Moan If statements are not very flexible, and sometimes quite difficult to use, but their simplicity is necessary to keep moan channels fast and make moan programs quick to compile.

Conclusion

That concludes the Advanced Moan Tutorial. You should now have an idea of how do use For loops, If statements and registers to write more advanced moan programs.

You may now benefit from going through the help pages for each of the moan commands.


Table of contents