MPL Training
Lesson 2
Black Panther

Variables

When it comes to writing any MPL program, you will need to use some type of
variables. There are quite a few to chose from, depending on what you will be
using it for.

Boolean       False/True       0..1
Char          Text             1 Character
String        Text             Sequence of 1..255 characters
Byte          Numerical        1..255
Integer       Numerical        -32767..32767
Word          Numerical        0..65535
LongInt       Numerical        -2147483648..214748364
Real          Numerical        9.99

A Boolean is going to be in one of two states. It is either true or false. (0
or 1) Booleans can be very handy if you are checking if a string contains 
specific words. Once one is found, you can set the boolean to true, and stop
checking for any of other words.

A Char is simply one character. If you have a menu in your program, you can use
a Char variable to hold the user's choice.

A String is a sequence of any characters, up to 255. This is useful if you are
reading a text file, such as a Mystic log file. You would read the line from
the file into a String, and then check if it contains the words your looking
for.

A Byte is a numerical value between 1 and 255. This is probably the one of the
most used numeric integers. There are times where you need to store larger 
numbers, but typically the byte is sufficient.

*Tip* You always want to use the smallest variable type you can for your use.
If you are only going to need to hold positive numbers up to 200, you could use
a LongInt, but it would use more memory than you really need to.

An Integer is also a popular variable in MPLs. It will hold anything from 
-32767 to 32767. It is a good 'catch-all', if your not sure which one you will
need.

A word will hold only positive numbers between 0 and 65535. It uses the same
amount of memory as an integer, so if you only need positive numbers, it is a
good choice.

LongInt is used when you will need very large numbers. I've used the LongInt in
some of the games I've written. It's good for storing gold or money in those
games, as those numbers can get quite large.

Real numbers aren't used a whole lot. This is used if you need your variable to
contain decimals.

Remember our example from the last lesson? Me either, let me grab a copy of it.

Uses
  User
  CFG

Var
  Age   : Byte
  Name  : String[20]
Begin
  WriteLn('What is your name?')
  Name:=Input(20,20,3,'')
  WriteLn('Hi '+Name+'! How old are you?')
  Age:=Input(2,2,10,'')
  WriteLn(Name+' are you really only '+Age+' years old?')
End

Right after the Uses section, we declared our variables. Byte was used for age
as I really didn't think we'd have someone older than 255 running this program.
But, did you notice what I did when I declared the string? Why did I put the 20
after it?

Well, as a string *can* contain up to 255 characters, there are times where you
know you won't need a string that long. In our case here, someone's first name
won't typically be longer than 20 characters. So, what I did, was tell the 
compiler that I wanted this to be a string, but it's only going to need to hold
up to 20 characters. The compiler will see that, and only set aside enough room
to hold those 20 characters, instead of using more memory that we need to. 

Also, when you know the value that you want the variable to hold in advance, 
you can do that when you declare it.

Var
  Age    : Byte=25
  Name   : String[20]='Sgt Pepper'
  
This is completed valid to do! There are times while using variables, you will
see compiler warnings about an integer not being assigned a value. This can 
easily be taken care of by assigning 0 (zero) to that variable until it is 
given a different value.

-=-=-=-=-=-=-=-=-

Arrays

This was something that took me a bit to wrap my head around. If you are going
to need a list of variables, let's say to read a bunch of user's ages from the
users.dat file. You could set up 300 different variables to use, or you could 
set up an array of byte variables.

Var
  Age     : Array[1..300] of Byte
  
is much easier to type in and work with than:

Var
  Age1    : Byte
  Age2    : Byte
  Age3    : Byte
  ..etc...
  
To access these variables, instead of just the name, as you normally would, you
would use Age[1], Age[200], etc. This make complicated tasks easier to 
accomplish as well. If we were reading the users.dat file to get the users 
ages, we would be running a loop that would read each record in the file. This
loop would already contain some type of counter. That counter number could be
used to save the users age. You would use the variable like: Age[Counter], 
instead of having a number in the square brackets.

Don't worry, this will make more sense as we go along. I also am purposely not
putting in examples of these scenarios at this time, as you'll end up getting
lost in the code.

There are times when you might need a multi-dimensional array. If you are 
making a chess game, and you need to be able to access each square of the 
board. You could set up the array of locations similar to how you would access
locations on the screen, using X,Y coordinates. This would be declared as:

Var
  Square   : Array[1..8,1..8] of Byte
  
Then you could use Square[1,1] as the upper left-hand square, and Square[8,8]
as the lower right-hand sqare, and so on. 

I hope this gives you a little better understanding of the different variables
available to use in MPL, and a bit of knowledge about arrays. Again, as we get
further into the lessons, there will be more explanations, and examples so 
you'll be able to see them in action.

I had wanted to get into records in this lesson, but I don't want the lessons 
to get too long. I also think that records deserve their own lesson, as there
is quite a bit of information to learn about them. They are used a lot, as they
organize the variables into groups, such as user records, or even last callers.
