[Main page] [Overview]     /lpc /BASIC /chapter2

LPC Basics
Written by Descartes of Borg
23 april 1993


CHAPTER 2
LPC Data Types

At this point, you should understand that objects in LPC are made up of
units called functions. The order of these functions is irrelevant.
The game uses the objects by loading them into memory and calling these
functions when needed. However, if you are reading this manual with
no prior programming experience, you may not know what a function is or
what it means to call one. And even if you have programming experience,
you may be wondering exactly how it is the function in an object just
added to the game gets called. Before any of these questions can be
answered for you, you will first however need to understand the concept
of a data type. Certainly the most boring subject in the manual, yet
the most crucial, as 90% of all errors (except misplaced {} and () :))
involve not properly understanding LPC data types. So bear through
this chapter, because once you understand it, you will find coding much
easier.

You possibly already know that computers cannot understand the letters
and numbers humans use. Instead, they understand thin s simply as 0's
and 1's. In order to execute your desires when you write a program in
any language, those instructions must be changed from alphabetic characters
and decimal numbers into 0's and 1's.

In any programming language, you have variables which you can manipulate.
These variables store data in memory. For example, you may have a
variable you call x. In the program, you will give x a value with
some statement like:
x = 65
This will allow you to issue instructions like:
write(x+"\n"); \n is symbolic for a carriage return
which will print the value of x to the screen, or:
y = x +5
which will allow you to assign to the variable y whatever the value of
x happens to be, plus 5.
The problem is that the computer does not know what '65' means. To the
computer, what you think of as 65 is:
00000000000000000000000001000001
But also, to the computer, the letter 'A' is also represented as
00000000000000000000000001000001
So when you instruct the computer to
write(x+"\n");
It must have some way of knowing whether you want to see:

65 or A

written out.
This is done through data types. In the above example in LPC,
you would have previously had the line somewhere in the code:
int x;
This tells the driver that whatever value is stored by x should be used
in the code as 65 and not as anything else.

That is a very basic to data types in general. The purpose of this
chapter however is to discuss LPC data types. What you should know
about data types in general is that they are used by the game driver
to determine how a value in memory should be used in the course of its
existence.

All LPMud drivers have the following data types:

int, string, object, int *, string *, object *, mixed *, void

Many (but not all) have the following data types. In spite of the fact
that they are not in every driver, they are important to discuss:

float, mapping, float *, mapping *

And a few drivers have the following rarely used data types which are not
pertinant to our discussion:

function, enum, struct, char

Simple data types:
The simplest data type in LPC is the trivial data type void. It has
no value of any kind. From the point of view of the LPC coder, the two
simplest data types are int and string. An int is any whole number.
Thus 1, 17, -42, 0, -10000023 are all ints. A string is one or more
alphanumeric characters. Thus "a", "We are borg", "-42", "This is a
string" are all strings. Strings are always enclosed in "". This allows
the driver, when reading code, to know the difference between things like
-42 and "-42", as well as to differentiate between variable names (like x)
and a string by the same name (like "x").

When you decide to use a variable in code, before you do anything with it,
you have to let the driver know what type it is so that it can use
the values it stores properly. This is done simply by placing the
data type before the variable name. This is called "declaring the variable".
Example:

void add_two_and_two() {
int x;
int y;

x = 2;
y = x + x;
}

At the beginning of the function, you are letting the driver know that
you will need memory space for 2 different int variables. Notice
that not only is this declaring important for letting the driver know
how to use the values, but it also helps the driver use memory more
effeciently, so that it sets aside only enough memotty o store any
int, and not memory for storing a string or an object (which take up
more memory). Once the driver sets aside memory for two ints and is
aware of how to handle the values it stores in those spaces, itcanc
go on and use them in the code.

For now we will leave the discussion with int, string, and void. The
more advanced data types will be covered in a later chapter.

SUMMARY:
For variables, the driver needs to know how much space to allocate to
store any values they hold and how to make use of those values in code
that refers to the variables. The simplest datapptyes in LPC are int,
void, and string. You do not have variables of type void, but it the
type does come into use when you talk about functions. Because variables
of different data types take up different amounts of memory, different
operations (such as +, -, etc) mean different things to the driver.
For example, in 5+5, the driver knows to add the values of 5 and 5 together
to create a new single value. With strings however, it does not do this.
With "a"+"b", it appends "b" to the end of "a" so you get "ab".
Errors can thus result if you try inadvertently to add "5"+5. Since
there is no operation which adds the values of strings (remeberb + with
regards to strings means append to), the driver will interpret this
as meaning a new string "55". If you wanted to get 10, you would therefore
have ended up with erroneous code.