MPL Training
Lesson 3
Black Panther

Records

When working with variables, you can set up an array, which is a group of the
same type of variables. But, what if you want a group, but it consists of many
different types of variables? We have an option available called a record. In
terms of Mystic, we can think of a record, such as the group of variables 
stored for each user on the BBS. These records include the users name (String),
security level (Byte), birthdate (LongInt), gender (Char), etc. It is very 
handy to be able to set up and use records, as they can be used to keep logical
variables together. In this example, in the user record.

When setting up a record, the process is very similar to declaring variables, 
with a few differences. The following is an example of a record definition.

Type
  PlyrRec = Record
  Name    : String[30]
  Gold    : LongInt
  HP      : Integer
  Armor   : Byte
  Weapon  : Byte
End

That tells the MPL compiler, there is a record of the following variables, and
it needs to be saved with the name of PlyrRec. But, before we can use this 
record, we still need to declare it. So, under out Var section of our code, we
would add something like:

Var
  Plyr : PlyrRec
  
Now, we can access the record by using the Plyr variable. When we need to 
access a specific variable within the record, we would do it like:

Plyr.Name:='Sam Smith'

First we need to tell it that we want to access the record, follow it with a 
period, then tell it which variable within the record we want. If we wanted the
players HP, we would use:

Plyr.HP:=0

Typically, if we were working on a game, you would only be working with one 
player at a time. However, there are many instances were you will need to 
access many records in the same run of your program. If we were working on 
an MPL to read the last 10 callers to our BBS, we would need to be able to hold
the users information for all 10. 

In this case, we could create an array of records. This is just telling the 
compiler that you want this record, but you will need to have 10 copies of it.

Var
  Plyr : Array[1..10] of PlyrRec
  
You will now have 10 records available for your use. In order to access these, 
you would need to use the array number, similar to how regular arrays are 
accessed.

So, instead of using:

Plyr.Name:='Sam Smith'

We would do it like:

Plyr[1].Name:='Sam Smith'
Plyr[2].Name:='Frank Zappa'

Records can get a little confusing at first, but once you get the hang of them,
they will save you hours of frustration.

I did want to share with you how my user records are set up in TDTA game I've 
been working on. I think this will give you an idea of how powerful records 
are, along with how versatile they are.

type ritems = Record
  iname       : string
  ihit_points : integer
  ihit_max    : integer
  ihit_multi  : real
  ifights_left: byte
  ihuman_left : byte
  iint_multi  : real
  idef_multi  : real
  istr_multi  : real
End

Type PlyrRec = Record
  Index        : Integer
  Name         : String[40]
  Alias        : String[40]
  hit_points   : longint   
  hit_max      : longint   
  hit_multi    : real      
  weapon_num   : byte      
  weapon       : string[20]
  seen_master  : boolean   
  fights_left  : Byte      
  human_left   : Byte      
  gold         : longint   
  bank         : longint   
  int_multi    : real      
  def          : longint   
  def_multi    : real      
  strength     : longint   
  str_multi    : real      
  level        : Byte      
  floor        : Byte      
  time         : longint   
  arm          : string[20]
  arm_num      : byte      
  dead         : Boolean   
  exp          : longint   
  sex          : Boolean   
  king         : byte      
  room         : Boolean   
  items        : Array[1..10] of ritems
End

Var
  Plyr : PlyrRec
  

I've included two record definitions here for you to look at. If you notice the
last item in the PlyrRec, 'items', is actually another record. So, each player
has a record, that contains 10 other records. In this case, they contain the 
items the player is carrying. I don't expect you to fully follow this, but this
is how you would access the player's items:

Plyr[1].items[5].iname:='pickles'

You first tell it the main variable (record) name, which is Plyr. In this case
we want it to read the first player record. Then look at the items the player
has, find the 5th record, and assign the iname variable to pickles. It looks
worse than it is. :)

