Blitz Basic / AmiBlitz generally uses syntax common to most dialects of BASIC. Line numbers are not used; instead, each line in the source file is executed in turn starting from the top. Lines are entered as with any text editor, and moving to a different line either by using the arrow keys, clicking the mouse, or pressing Enter, tells Blitz to scan the line and detect any keywords for highlighting.
Blitz treats each line as a separate statement, so the end of the line of text is treated as the end of the command. Once a line is executed, program flow continues with the next line down. Blank lines are ignored, as are comments. More than one command can be placed on one line if required by separating the commands with a colon, for example:
a = b + c : NPrint a
Variables are individual "boxes" in memory in which you can store a single piece of information. They are assigned a value using a single equals sign. For example:
a = 10
b = a + 5
In this example, a contains the value 10 and b contains the value 15.
Variables can be named whatever you like, so long as the following rules are followed:
- Their names are not the same as any built-in keywords, or any procedures or labels defined in your code
- Their names do not begin with a number (they can contain numbers however)
- Their names do not contain spaces
- Their names do not contain special characters other than underscore (or $ for strings)
It is good practice to name variables with a name that reflects their function. This might be a little longer to type, but it will make your code much easier to read and follow later on! For example, CurrentPlayer and Player1Score make much more sense than a and b.
In AmiBlitz 3, keywords by default will be highlighted as follows:
- Built-in keywords are highlighted in blue
- Built-in keywords that access the custom chips are highlighted in red (these will only work when running on classic Amiga hardware or an emulator)
- Comments are highlighted in green italics
- Constants are highlighted in magenta
- Procedures (statements and functions) are highlighted in green
- String constants are highlighted in grey
Statements are commands built into Blitz are keywords that are entered to carry out a specific action. They may accept parameters or arguments which are entered after the keyword and separated by a comma if more than one argument is required. Brackets are not used around the arguments. For example:
The Plot command plots a single pixel on the currently used bitmap, and it takes 3 arguments. The first is the X coordinate, the second is the Y coordinate, and the third is the colour in which to draw the pixel. So, this example draws a single pixel 20 pixels across and 30 pixels down using pen 2 on the current bitmap.
Functions are similar to statements except they return a value to your program, which means you must do something with the value even if you don't need it. To differentiate from commands, arguments to functions need to be enclosed in brackets. For example:
a = Abs(-25)
The Abs function calculates and returns the absolute value of its argument, in this case 25. The value returned is stored in the variable a. The return value can also be used directly as a parameter in another function or statement, for example:
NPrint "The absolute value of -25 is ", Abs(-25)
This example uses the result of the Abs function as the second argument of the NPrint command. NPrint then displays the message followed by the result of the Abs function.
Note that these functions are different to procedures that return values (also called functions).
Comments are extra text entered in your code that aren't part of the program itself but are used to make notes within your code. Blitz follows the same convention as AmigaDOS in using the semicolon character to mark the start of a comment. The comment ends at the end of the line. For example:
- Calculate the Absolute value of variable a
b = Abs(a)
- Now tell everyone
Nprint "Absolute value of ", a, " is ", b
Comments can also be added after the working part of a line if desired:
Plot x, y, 3 ; Drawing colour 3 at the coordinates x,y
Comments are optional, but highly recommended! In a simple program they might not seem necessary, but once your code gets longer and more complicated, comments can help enormously when you try to understand what you were doing previously. They're even more important if there's ever a possibility that someone else will work on your code!
Labels allow you to jump to or identify segments of your code. They are used as the targets for Goto and Gosub statements, as well as more advanced techniques such as determining the address of a segment of code in memory.
Labels are created by entering the name of the label at the start of a line. Each label must be unique, and it is considered good practice to end the label with a colon, for example:
Optionally, a dot can be added at the start of the label. This indicates to the editor that the label should be added to the list of shortcuts in the side navigation bar of the editor screen. For example:
.MyLabelThis is very convenient, as it allows you to click on a label to move the editor directly to its location.
Neither the colon nor the dot count as part of the actual label, so it is still referenced as MyLabel in both of the above examples.
AmiBlitz 3 adds the possibility to use locally unique labels, which attaches a local label to the most recent normal label in order to create a unique label that can be repeated throughout your program. Local labels are identified by putting an apostrophe before the name. For example:
NPrint "This is Routine 1"
x = a + b
.routine2 NPrint "This is Routine 2" 'maincode NPrint "Message here" In this example, both routines contain the label maincode, which would normally cause an error. However, the apostrophe declares them as local, and so they get joined with the previous normal label to create the labels routine1maincode and routine2maincode. This can be useful if you have multiple similar routines that you would like to keep consistent across routines.